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:
"""
Imports an Image
TESTED REVIT API: -
Author: Gui Talarico | github.com/gtalarico
This file is shared on www.revitapidocs.com
For more information visit http://github.com/gtalarico/revitapidocs
License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md
"""
# See: http://www.revitapidocs.com/2015/05c3dbe2-fe7e-c293-761d-b11f356a011b.htm
from clr import StrongBox
from Autodesk.Revit.DB import XYZ, ImageImportOptions, BoxPlacement, BuiltInParameter
# Import Options
import_options = ImageImportOptions()
import_options.Placement = BoxPlacement.Center
This file has been truncated. show original
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
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
If you restart dynamo, this fixes the error ?
1 Like
@c.poupin ,
i restarted revit alsoā¦
but the error changed
importDWG_V01.dyn (8.9 KB)
I donāt know, itās very strange, maybe try to reinit the Cpython3 engine
1 Like
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