Hello,
I have a 3D .dwg file. I would like to create an Inventor part file (.ipt) from it with the use of Inventor API. Any language would help (VBA, VB.NET, C#...).
The 3D .dwg file contains only a single autocad solid, not lines or curves.
Is there such an API example?
I would be grateful for any kind of help.
Solved! Go to Solution.
Solved by Michael.Navara. Go to Solution.
Hi @george1985
This sounds like a difficult task to do with API especially working with geometry not created in inventor. Any reason why your not doing this manually? Can you attach a sample file to work with and an image of the part in question?
Hi @A.Acheson
Thank you for replying.
I have tens and tens of .dwg 3D models created in Autocad by external party. I have to "import" all of them to particular Inventor assemblies as single parts. What I am doing right now is opening each .dwg file in Inventor, and saving it as an .ipt. And then inserting it into appropriate assembly.
It would save me hours of work, if I could run a VBA/VB.NET/C# code, which will create .ipt files from all those .dwg files. Then I can easily open particular assembly file and just "Place" the needed .ipt.
I attached a 3D model of one such .dwg file.
I try to import DWG model, but with controversial result.
Here is my code for import single DWG model
Sub ImportSingleDwg()
Dim dwgFile = "C:\DWG\autocad_3D.dwg"
Dim dwgTranslatorId = "{C24E3AC2-122E-11D5-8E91-0010B541CD80}"
Dim dwgTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById(dwgTranslatorId)
Dim dataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium()
dataMedium.FileName = dwgFile
Dim context As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext()
context.Type = IOMechanismEnum.kDataDropIOMechanism
Dim options As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
'*** NOTES / WARNINGS ***
'1) HasOpenOptions returns always FALSE
'2) Without this call the export doesn't work for me
'3) Variable 'options' contains one value named 'Import_Acad_IniFile' but when I try to change them, export doesn't work
Dim hasOpenOptions As Boolean = dwgTranslator.HasOpenOptions(dataMedium, context, options)
Logger.Debug("OpenOpts: {0}", hasOpenOptions)
Logger.Debug(options.Count)
Logger.Debug(options.Name(1))
Logger.Debug(options.Item(1))
Dim targetObject As Object
dwgTranslator.Open(dataMedium, context, options, targetObject)
Logger.Debug("Done")
End Sub
Tested on Win 10 CZ + Inventor 2024 CZ
I hope it helps.
Thank you very much @Michael.Navara !
I have Inventor 2022. The translator addin id should be the same as on your Iventor 2024?
"{C24E3AC2-122E-11D5-8E91-0010B541CD80}"
Yes, this is constant value for Addin
Here is fixed code to import multiple DWG model, but with previously mentioned obscurities.
Sub ImportMultipleDwgModels()
Dim iniFileName As String = "C:\DWG\importDwg.ini"
Dim dwgFileNames As String() = {
"C:\DWG\autocad_3D_1.dwg",
"C:\DWG\autocad_3D_2.dwg",
"C:\DWG\autocad_3D_3.dwg",
"C:\DWG\autocad_3D_4.dwg"
}
For Each dwgFileName As String In dwgFileNames
Dim partDocument As PartDocument = ImportDwgModel(dwgFileName, iniFileName)
If partDocument Is Nothing Then Continue For
Dim iptFileName = System.IO.Path.ChangeExtension(dwgFileName, ".ipt")
'Dim iptFileName = dwgFileName & ".ipt"
partDocument.SaveAs(iptFileName, False)
partDocument.Close()
Next
End Sub
Function ImportDwgModel(dwgFileName As String, iniFileName As String) As PartDocument
'Get DWG Translator
Dim dwgTranslatorId = "{C24E3AC2-122E-11D5-8E91-0010B541CD80}"
Dim dwgTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById(dwgTranslatorId)
'Create DataMedium
Dim dataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium()
dataMedium.FileName = dwgFileName
'Create translation context
Dim context As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext()
context.Type = IOMechanismEnum.kDataDropIOMechanism
'Create options
Dim options As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
Dim hasOpenOptions As Boolean = dwgTranslator.HasOpenOptions(dataMedium, context, options)
'options.Value("Import_Acad_IniFile") = iniFileName
'Import DWG
Dim targetObject As Object
dwgTranslator.Open(dataMedium, context, options, targetObject)
'3D model is imported as side effect not as primary output
Dim primaryOutputDoc = TryCast(targetObject, Document)
If Not primaryOutputDoc Is Nothing Then primaryOutputDoc.Close(True)
'Get imported model document from opened documents by it's DisplayName
Dim displayName As String = System.IO.Path.GetFileNameWithoutExtension(dwgFileName)
Dim dwg3dPart As PartDocument = Nothing
For Each document As Document In ThisApplication.Documents
If document.DisplayName.StartsWith(displayName) Then
dwg3dPart = TryCast(document, PartDocument)
If not dwg3dPart Is Nothing Then Exit For
End If
Next
If Not dwg3dPart Is Nothing Then Return dwg3dPart
Logger.Error("Import failed: {0}", dwgFileName)
Return Nothing
End Function
@Michael.Navara Thank you very much for the new version as well!
Would you mind if I ask how to run this VBA code?
So in Inventor: Tools->VBA Editor.
Then once VBA Editor window appears, I should choose: File->New Project. Then inside the 'Module' of that new project, I should paste your VBA code.
I commented out the 'Dim iniFileName' and adapted the 'dwgFileNames' paths to my PC.
Then I hit 'Run' button.
When I do this, I get message:
"Compile error:
Syntax error"
I apologize for disturbing you with such trivialities, even though you made the programming code I needed.
This is not a VBA code but iLogic code. You need to create new external iLogic rule, copy code into and you need to add
Sub Main()
ImportMultipleDwgModels()
End Sub
at the top of the rule
Thank you very much for the invaluable help @Michael.Navara .
I owe you a beer for this. If you have some online Patreon-like account, please let me know.
Can I try to be annoying with only one more question?
In the mean time, I tried to call your code externally from SharpDevelop (Visual Studio-like editor).
On the line:
dwgTranslator.Open(dataMedium, context, options, targetObject)
the following error is raised:
"Message: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
Do you have suggestion what could be the issue?
Some Inventor.Interop methods can only be called internally through iRule or VBA Editor, but not "outside" from Inventor with the use of Autodesk.Inventor.Interop.dll?
--------------------
EDIT: I didn't have one line included:
context.Type = IOMechanismEnum.kDataDropIOMechanism
This cause the error message. Now that included it, it works.
Thank you very much for the help Michael!
Can't find what you're looking for? Ask the community or share your knowledge.