Hi All,
I am having problems with a code.
I am trying to make a dxf from each sheet metal part in an assembly with ilogic: (I have cut and and paste diffrent codes from this forum in an poor atempt...)
Sub Main
Dim oApp As Inventor.Application = ThisApplication
Dim oAssy As Inventor.AssemblyDocument = oApp.ActiveDocument
For Each oSubDoc as Inventor.Document In oAssy.AllReferencedDocuments
If oSubDoc.DocumentType = kPartDocumentObject Then
Dim oPartPropset As Inventor.PropertySet = oSubDoc.PropertySets("Design Tracking Properties")
Dim oPartParam As Inventor.PropertySet = oSubDoc.PropertySets("Inventor User Defined Properties")
Dim oParameter As Parameter
For Each oParameter In oSubDoc.ComponentDefinition.Parameters
oParameter.ExposedAsProperty = True
Next oParameter
If oSubDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
DefaultChoice = True
CADline()
Else
' here is code for normal parts
' .................
End If
End If
Next
oAssy = Nothing
oApp = Nothing
End Sub
Public Sub CADline()
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oDoc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold
Else
oCompDef.FlatPattern.Edit
End If
Dim sOut As String
sOut = "FLAT PATTERN DXF?AcadVersion=R12&OuterProfileLayer=IV_INTERIOR_PROFILES"
Dim sFname As String
sFname = oDoc.FullFileName
sFname = Left$(sFname, Len(sFname) - 3) & "dxf"
oCompDef.DataIO.WriteDataToFile( sOut, sFname)
oDoc = ThisApplication.ActiveDocument
Dim oSMDef As SheetMetalComponentDefinition
oSMDef = oDoc.ComponentDefinition
oSMDef.FlatPattern.ExitEdit
End Sub
Solved! Go to Solution.
Solved by c.rp.henderson. Go to Solution.
Hi Anders,
If you are having issues it's worth pointing out what the issue is. Are you getting any errors?
I don't think you have to start editing the flat pattern in order to save it to DXF.
Have a look at this code: http://modthemachine.typepad.com/my_weblog/2009/04/controlling-layers-in-your-flat-pattern-export.ht...
Cheers,
Hi Adam,
I am getting the following eroor message, can you help me out?
Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.PartDocument'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{29F0D463-C114-11D2-B77F-0060B0F159EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
//
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.PartDocument'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{29F0D463-C114-11D2-B77F-0060B0F159EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at LmiRuleScript.CADline()
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
I know I am replying to an old post but I am finding very little on this subject. Most ilogic/dxf code I can find is only at the part level.
I am happy to report success! After much effort I have managed to cobble together code to create a DXF file for every sheet metal part in an assembly (and Sub assemblies).
While that is working well I have two things to iron out.
I am looking for help adapting the configuration .ini file to the .WriteDataToFile string. If anyone has done that please send me pointers, I would be grateful!
The other challenge has to do with sheet metal parts that do not have a flat pattern. Does anyone know of a rule that will create flat patterns for all sheet metal parts in an assembly? I would love to add that my code.
Right now the code will create a flat pattern for one part but a second .ipt that is missing a flat pattern will cause an error. Because it does make one flat pattern I can just keep running it until everything has a flat pattern but that is not ideal.
A better way is for me to do a better job of make flat patterns as I model so that when the assembly is done there aren't any flat patterns missing.
Feel free to message me if you are interested in the code I have put together.
Thanks in advance!
I may be able to help with creating the flat patterns, could you post or send me the code?
Yes send me the code and I'll have a squint at how to automate the flat pattern creation if a part doesn't have one.
Second try - I sent the code hours ago, even click on the attachment link and opened it from the post. now that post is gone?
Anyway, here it is again. Right now I just tell the user what files are skipped due to lack of flat pattern, they can fix those few parts and run again. It would be great to have it make the flats too! Thanks in advance for the help
In the place where you have a message box to warn of a missing flat pattern, use this instead:
'checking for parts that don't have flat pattern and creating flat pattern if none exist If oCompDef.HasFlatPattern = False Then oCompDef.Unfold() oCompDef.FlatPattern.ExitEdit() End If
It will create a flat pattern for the part then continue onto your Rev add-on section.
Sot his full code doesn't work?
Sub Main() Dim New_Folder_Path As String = "G:\DXF\Production\" & iProperties.Value("Project", "Project") Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisApplication.ActiveDocument If iProperties.Value("Project", "Project") = "" Then MsgBox("Add Job# to iProperties and try again.",,"Job# Missing") Exit Sub End If Dim Job_confirm as String Job_confirm = MsgBox ("Sending Files to DXF Folder " & iProperties.Value("Project", "Project"),vbOKCancel, "Confirm Job #") If Job_Confirm = "2" Then Exit Sub My.Computer.FileSystem.CreateDirectory(New_Folder_Path) 'Ask user for REV level Dim Rev_Level as String = "" Rev_Level = InputBox ("Enter Rev Level","Creating DXF files For Entire Assembly") Dim Count_up As Integer = 0 Dim Gauge_Folders(12) As String Dim Ga As String Dim doc as document For Each doc In oAsmDoc.AllReferencedDocuments If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Dim oSheetMetalCompDef As SheetMetalComponentDefinition oSheetMetalCompDef = doc.ComponentDefinition Ga = oSheetMetalCompDef.ActiveSheetMetalStyle.Name Ga = Replace(Ga, "/", "_") Ga = Replace(Ga, Chr(34), "") If UBound(Filter(Gauge_Folders, Ga)) > -1 Then Else Gauge_Folders(Count_up) = Ga Gauge_Path = New_Folder_Path & "\" & Ga My.Computer.FileSystem.CreateDirectory(Gauge_Path) End If End If Next ReDim Preserve Gauge_Folders(Count_up) For i = LBound(Gauge_Folders) To UBound(Gauge_Folders) msg = msg & Gauge_Folders(i) & vbNewLine Next i MsgBox ("Adding DXF files to Folders:" & vbNewLine & vbNewLine & msg, ,"Gauges Found") Count_Up = 0 For Each doc In oAsmDoc.AllReferencedDocuments If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Call Make_DXF(doc, Rev_Level, New_Folder_Path) End If Count_up = Count_up + 1 Next End Sub Sub Make_DXF(doc as Document, Rev_L As String, File_Location As String) Dim oSheetMetalCompDef As SheetMetalComponentDefinition oSheetMetalCompDef = doc.ComponentDefinition Dim GA_Adder As String = oSheetMetalCompDef.ActiveSheetMetalStyle.Name GA_Adder = Replace(GA_Adder, "/", "_") GA_Adder = Replace(GA_Adder, Chr(34), "") Dim Part_Name As String = doc.DisplayName Dim TestPos as Integer = 0 Dim oCompDef As SheetMetalComponentDefinition oCompDef = doc.ComponentDefinition If oCompDef.HasFlatPattern = False Then oCompDef.Unfold() oCompDef.FlatPattern.ExitEdit() Else Dim Rev_Adder As String = "" If Rev_L <> "" Then Rev_Adder = " REV " & Rev_L TestPos = InStr(1, Part_Name, ".") If TestPos <> 0 Then Part_Name = Left(Part_Name, InStr(Part_Name, ".")-1) End If Dim New_Name As String = Part_Name ' '*******************remove line below to remove gauge from file name*********** New_Name = GA_Adder & " " & New_Name ' '*******************/remove line above to remove gauge from file name********** Dim nameandpath As String = File_Location & "\" & GA_Adder & "\" & New_Name & Rev_Adder & ".dxf" Dim oDataIO As DataIO = doc.ComponentDefinition.DataIO oDataIO.WriteDataToFile("FLAT PATTERN DXF?AcadVersion=R12&RebaseGeometry=True&SimplifySpline=True&InteriorProfilesLayer=IV_INTERIOR_PROFILES&InvisibleLayers=IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_TOOL_CENTER_DOWN;IV_ARC_CENTERS;IV_FEATURE_PROFILES;IV_FEATURE_PROFILES_DOWN;IV_UNCONSUMED_SKETCHES;IV_ROLL_TANGENT;IV_ROLL&SplineToleranceDouble=0.01",nameandpath) End If End Sub
It is very possible that I am missing something but no, I created a new rule and pasted the full code from your post and have the same error.
I am left with a part open (instead of the assembly) and the Unspecified error message.
If the assembly only has one part that is missing the flat pattern it works just fine. However, when the code finishes the part that needed the flat is now the active file on my screen instead of the assembly. Do I need to make the assembly active between each flat created?
Anyway, it seems to be the second flat pattern that is the issue right now.
Thanks for the help.
For reasons unknown to me, to avoid the error I had to change the code to:
That should work for you now. Let me know if it doesn't.
Sub Main() Dim New_Folder_Path As String = "G:\DXF\Production\" & iProperties.Value("Project", "Project") Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisApplication.ActiveDocument If iProperties.Value("Project", "Project") = "" Then MsgBox("Add Job# to iProperties and try again.",,"Job# Missing") Exit Sub End If Dim Job_confirm as String Job_confirm = MsgBox ("Sending Files to DXF Folder " & iProperties.Value("Project", "Project"),vbOKCancel, "Confirm Job #") If Job_Confirm = "2" Then Exit Sub My.Computer.FileSystem.CreateDirectory(New_Folder_Path) 'Ask user for REV level Dim Rev_Level as String = "" Rev_Level = InputBox ("Enter Rev Level","Creating DXF files For Entire Assembly") Dim Count_up As Integer = 0 Dim Gauge_Folders(12) As String Dim Ga As String Dim doc as document For Each doc In oAsmDoc.AllReferencedDocuments If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Dim oSheetMetalCompDef As SheetMetalComponentDefinition oSheetMetalCompDef = doc.ComponentDefinition Ga = oSheetMetalCompDef.ActiveSheetMetalStyle.Name Ga = Replace(Ga, "/", "_") Ga = Replace(Ga, Chr(34), "") If UBound(Filter(Gauge_Folders, Ga)) > -1 Then Else Gauge_Folders(Count_up) = Ga Gauge_Path = New_Folder_Path & "\" & Ga My.Computer.FileSystem.CreateDirectory(Gauge_Path) End If End If Next ReDim Preserve Gauge_Folders(Count_up) For i = LBound(Gauge_Folders) To UBound(Gauge_Folders) msg = msg & Gauge_Folders(i) & vbNewLine Next i MsgBox ("Adding DXF files to Folders:" & vbNewLine & vbNewLine & msg, ,"Gauges Found") Count_Up = 0 For Each doc In oAsmDoc.AllReferencedDocuments If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Call Make_DXF(doc, Rev_Level, New_Folder_Path) End If Count_up = Count_up + 1 Next End Sub Sub Make_DXF(doc as Document, Rev_L As String, File_Location As String) Dim oSheetMetalCompDef As SheetMetalComponentDefinition oSheetMetalCompDef = doc.ComponentDefinition Dim GA_Adder As String = oSheetMetalCompDef.ActiveSheetMetalStyle.Name GA_Adder = Replace(GA_Adder, "/", "_") GA_Adder = Replace(GA_Adder, Chr(34), "") Dim Part_Name As String = doc.DisplayName Dim TestPos as Integer = 0 'checking for parts that don't have flat pattern and telling user what parts are skipped for lack of flat pattern part = doc.ComponentDefinition.Document.FullDocumentName ' Create flat pattern if none If Not oSheetMetalCompDef.HasFlatPattern() Then ' Open document openDoc = ThisApplication.Documents.Open(part,False) ' Unfold part oSheetMetalCompDef.Unfold() ' Exit flat pattern oSheetMetalCompDef.FlatPattern.ExitEdit() ' Close document openDoc.Close() End If Dim Rev_Adder As String = "" If Rev_L <> "" Then Rev_Adder = " REV " & Rev_L TestPos = InStr(1, Part_Name, ".") If TestPos <> 0 Then Part_Name = Left(Part_Name, InStr(Part_Name, ".")-1) End If Dim New_Name As String = Part_Name ' '*******************remove line below to remove gauge from file name*********** New_Name = GA_Adder & " " & New_Name ' '*******************/remove line above to remove gauge from file name********** Dim nameandpath As String = File_Location & "\" & GA_Adder & "\" & New_Name & Rev_Adder & ".dxf" Dim oDataIO As DataIO = doc.ComponentDefinition.DataIO oDataIO.WriteDataToFile("FLAT PATTERN DXF?AcadVersion=R12&RebaseGeometry=True&SimplifySpline=True&InteriorProfilesLayer=IV_INTERIOR_PROFILES&InvisibleLayers=IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_TOOL_CENTER_DOWN;IV_ARC_CENTERS;IV_FEATURE_PROFILES;IV_FEATURE_PROFILES_DOWN;IV_UNCONSUMED_SKETCHES;IV_ROLL_TANGENT;IV_ROLL&SplineToleranceDouble=0.01",nameandpath) End Sub
The code works flawlessly now! Thank you - Kudo given.
Out of curiosity, did the other way work without an error on a different version of Inventor? If so, what was that version?
Thanks again, I consider this a complete solution now!
Glad to hear it
It was working for me, but then I closed the assembly, opened it back up again, ran the rule and it gave me the error.
From there, I then opened all the sheet metal parts manually, closed them, ran the code and it worked without any error. The code would then run fine until I closed the assembly and opened it back up again.
Seems like a bug to me, but hopefully the workaround of replicating that opening and closing of each file through the code is not too laborious.
A very high percentage of our parts will have flat patterns before we make dxf files so hitting a few that need a flat (open and close) is no big deal. Gosh, compared to the old way I wouldn't mind having to open and close 100 parts. Thanks again!