Import .dwg to generic model, how?

Hello,

whats wrong in my code ? i want to import a .dwg to a family template

import sys
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Preparing input from dynamo to revit
cad = IN[0]
view = UnwrapElement(IN[1])

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

options = DWGImportOptions()
id = clr.StrongBox[ElementId](ElementId(271182))
bool = doc.Import(cad, options, view, id)

TransactionManager.Instance.TransactionTaskDone()

OUT = "Done"

Maybe because the file path is not raw? like this ā€˜C:\path\to\image.jpgā€™ ?

Maybe this will help. This code is for importing an image, but its in the same method tree:

1 Like

Looks like your method isnā€™t seeing the element id input. Verify that is returning correctly, as this may be a CPython issue, in particular with the various data types for overloads could be causing a problem here.

Check if thatā€™s constructing correctly.

@leonard.moelders @jacob.small

the import via image or change properties does not workā€¦

i tried to access ā€œStrongBoxā€ ā€¦ but without success

import sys
import clr

clr.AddReference('RevitAPI')
from clr import StrongBox
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *


clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Preparing input from dynamo to revit
cad = IN[0]
view = UnwrapElement(IN[1])

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
#StrongBox[Element]()
options = DWGImportOptions()
id = clr.StrongBox[Element](ElementId(23))
bool = doc.Import(cad, options, view, id)

TransactionManager.Instance.TransactionTaskDone()

OUT = "Done"

what the hell is a StrongBox ? for what need i an Id ? which Id is it?

Strongbox is a way to refer to a reference of an object in memory which has an explicit type, in this case a Revit element id. You can read more about this method in the documentation page.

The reason for requiring an element ID isnā€™t clear to me, but it may have been intended as a way to manage DWGs back in the day - allowing API updates more directly. Iā€™d use a value of -1 for now, which is an invalid element ID, as that seems to reset the ID to a new value.

Iā€™m not sure how to reference a strongbox object in CPython, but your original code (with the change to element ID) runs fine in IronPython.

Maybe @Gui_Talarico can explain it further for you as it is his code :slight_smile:

Here is the correct syntax with CPython3 engine (PythonNet) for method with ā€˜out parametersā€™

import sys
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Preparing input from dynamo to revit
cad = IN[0]
view = UnwrapElement(IN[1])

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

options = DWGImportOptions()
dummyId = ElementId.InvalidElementId
result_import, cad_instance_Id = doc.Import(cad, options, view, dummyId)

TransactionManager.Instance.TransactionTaskDone()

OUT = result_import, doc.GetElement(cad_instance_Id)

https://pythonnet.github.io/pythonnet/python.html#out-and-ref-parameters

1 Like

@c.poupin ,

what does the error mean?

I have never had this error before :open_mouth:
If you restart dynamo, this fixes the error ?

1 Like

@c.poupin ,

i restarted revit alsoā€¦ :frowning:
but the error changed
grafik

importDWG_V01.dyn (8.9 KB)

I donā€™t know, itā€™s very strange, maybe try to reinit the Cpython3 engine

image

1 Like

@c.poupin ,

same result :frowning:

I canā€™t reproduce the bug

@c.poupin ,

i use core 2.13. should i upgrade? which is stable?

I used core 2.13 (Revit 2023) too for this test

1 Like

Do you have update latest version hotfix 2023 yet?

1 Like

@chuongmep ,

i did the hotfix! but still not working

i work right now with 2.16

This is Test in my side , also donā€™t have any problem like that :


But it just is dynamo version 2.13

https://dictionary.dynamobim.com/#/Revit/Views/View/Action/IsViewTemplate

it is quite possible that I am off the mark
cordially
christian.stan