Ilogic - Batch dxf from Assembly

Ilogic - Batch dxf from Assembly

Anonymous
Not applicable
4,415 Views
32 Replies
Message 1 of 33

Ilogic - Batch dxf from Assembly

Anonymous
Not applicable

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
Accepted solutions (1)
4,416 Views
32 Replies
Replies (32)
Message 2 of 33

adam.nagy
Autodesk Support
Autodesk Support

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,



Adam Nagy
Autodesk Platform Services
0 Likes
Message 3 of 33

anders.klahr
Explorer
Explorer

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)

0 Likes
Message 4 of 33

kevinv
Enthusiast
Enthusiast

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!

Message 5 of 33

Anonymous
Not applicable

I may be able to help with creating the flat patterns, could you post or send me the code?

0 Likes
Message 6 of 33

kevinv
Enthusiast
Enthusiast
Wow, thank you for the reply. I really had given up hope that anyone out there was doing this and understood how to do this. Good news, I have worked my way through the problem and I am in awe of how well this solution works. The equipment I design uses many (think hundreds) different sheet metal parts. Everything from 12ga CRS to 20ga 304SS. After completing a project I would sometimes spend the better part of a day (or more than a day on big projects) just making DXF files.

Now that I have worked out assembly level dxf creation that pain is gone. We even made the sheetmetal department happy. My ilogic code adds the sheet metal rule name to the dxf file, and sorts the files per job into folders based on sheet metal rule name. They tell me it is awesome for nesting and their work-flow.

Sorry to chew your ear but very few here understand what a big deal this is and it is fun to share it with someone.

Anyway, I am happy to send my code if you want to see it or if it would help you in any way but I am really pleased with how it is working for us.

Thanks again for your reply!

Kevin
0 Likes
Message 7 of 33

Anonymous
Not applicable

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.

0 Likes
Message 8 of 33

kevinv
Enthusiast
Enthusiast

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

Message 9 of 33

Anonymous
Not applicable

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.

0 Likes
Message 10 of 33

kevinv
Enthusiast
Enthusiast
Thanks for the help, I think I may have tried something like this as I am getting an error that I got before when I tried to get this working.
It will create the flat for the first one but the second part without a flat pattern gets this error:
System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.SheetMetalComponentDefinition.Unfold()
at LmiRuleScript.Make_DXF(Document doc, String Rev_L, String File_Location)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
Any ideas?
0 Likes
Message 11 of 33

Anonymous
Not applicable
Out of interest what version of Inventor are you using?
0 Likes
Message 12 of 33

kevinv
Enthusiast
Enthusiast
Good question, currently 2017 Professional, soon moving to 2018
0 Likes
Message 13 of 33

Anonymous
Not applicable
Any chance you could attach an example assembly with a few sheet metal parts in it? Its working on my PC, but it would be good to try it with your own files to see if it still works for me. I have a copy of Inventor 2017 Pro here so I could give it a try tomorrow morning.
0 Likes
Message 14 of 33

kevinv
Enthusiast
Enthusiast

I made a super simple assembly just to run this test.  All the same parts but some do not have flat patterns.

 

Thanks again for the help.

0 Likes
Message 15 of 33

MechMachineMan
Advisor
Advisor

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

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 16 of 33

kevinv
Enthusiast
Enthusiast

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.

0 Likes
Message 17 of 33

Anonymous
Not applicable
Accepted solution

For reasons unknown to me, to avoid the error I had to change the code to:

  1. Open each part that didn't have a flat pattern
  2. Create the flat pattern then exit the flat pattern
  3. Close the part

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

 

Message 18 of 33

kevinv
Enthusiast
Enthusiast

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!  

0 Likes
Message 19 of 33

Anonymous
Not applicable

Glad to hear it Smiley Happy

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.

0 Likes
Message 20 of 33

kevinv
Enthusiast
Enthusiast

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!

0 Likes