How to create flat patterns and export them to DXF from assemblies

How to create flat patterns and export them to DXF from assemblies

mindaV3J2J
Enthusiast Enthusiast
884 Views
10 Replies
Message 1 of 11

How to create flat patterns and export them to DXF from assemblies

mindaV3J2J
Enthusiast
Enthusiast

Hi!

 

Jus tas the title says, I want to program a way to, firstly, create flat patterns of each part from assemblies, and then export them to dxf. I'm extremely new to the API and dont really know where to start. Can anyone point me in the right direction?

 

I currently have the following code to connect inventor with the VBA interface. 

 

Dim oDrawing As Inventor.AssemblyDocument
Set oDrawing = ThisApplication.ActiveDocument

Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oDoc.ComponentDefinition

If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold

Else

 

 

 

 

I've also tried looking at past posts, but many of them have prior knowledge in this area as well. I come from a prior background in python and C++.

0 Likes
Accepted solutions (1)
885 Views
10 Replies
Replies (10)
Message 2 of 11

A.Acheson
Mentor
Mentor

Hi @mindaV3J2J 

Here is an example using ilogic which has VB.NET as the underlining launguage. I used this keyword when doing an internet search

"Export DXF flat pattern from Assembly"

 

There is many more samples on this forum utilizing ilogic so I would suggest to use that launguage over VBA if your starting out. Knowing each one is also an asset too. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 11

mindaV3J2J
Enthusiast
Enthusiast

Thanks for the help. I've looked at this and several similar ones; however, in the VBA code, I cannot use Dim and set something in the same line, which leads me to believe I'm using a different version than many users here. Is this the case?

 

The error message is that it expects end of statement.

0 Likes
Message 4 of 11

A.Acheson
Mentor
Mentor

Yes for VBA you cannot declare and set the object variable in the same line. So if you likely have ilogic/ VB.Net code which you will need to translate. Please attach the code your using and any error message you have for help in diagnosing the issues. 

 

The alternative is to open the ilogic editor and just paste in  and run the rule from a form or similar.  

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 11

mindaV3J2J
Enthusiast
Enthusiast

Sub test()


'Set your filepath here:
SETFilePath = "C:\Temp"

 

'Check for flat pattern >> create one if needed
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.AssemblyDocument
Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oDoc.ComponentDefinition
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold

Else
oCompDef.FlatPattern.Edit
End If

'DXF Settings
Dim sOut As String
Dim sPATH As String
sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_INTERIOR_PROFILES"
Dim sFname As String
sFname = SETFilePath & "\" & ThisDoc.FileName(False) & ".dxf"

'Export the DXF and fold the model back up
oCompDef.DataIO.WriteDataToFile sOut, sFname
Dim oSMDef As SheetMetalComponentDefinition
oSMDef = oDoc.ComponentDefinition
oSMDef.FlatPattern.ExitEdit

End Sub

 

This is my current code, I found it from another post and tweaked it a little. The first part where things go wrong are i the first set statement-I've had trouble casting my oDoc variable as an assembly document.

 

Thanks so much in advance

0 Likes
Message 6 of 11

mindaV3J2J
Enthusiast
Enthusiast

ive done some messing around with it and heres my new version, issues still remoain 

 

Sub ExportFlatPatternToDXF2()

    Dim SETFilePath As String
    SETFilePath = "PATH"

 

    
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MsgBox "Please open an assembly document.", vbExclamation, "iLogic"
        Exit Sub
    End If

 

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

 

    Dim oCompDef As SheetMetalComponentDefinition
    Dim oComponent As ComponentOccurrence

 

    
    For Each oComponent In oDoc.ComponentDefinition.Occurrences

        On Error Resume Next
        Set oCompDef = oComponent.Definition
        On Error GoTo 0

 

        If Not oCompDef Is Nothing And oCompDef.Type = kSheetMetalComponentDefinitionObject Then

            If oCompDef.HasFlatPattern = False Then
                oCompDef.Unfold
            Else
                oCompDef.FlatPattern.Edit ->> issue
            End If

 

            
            Dim sOut As String
            Dim sFname As String
            sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_INTERIOR_PROFILES"
            sFname = SETFilePath & "\" & oComponent.Name & ".dxf"

 

            
            oCompDef.DataIO.WriteDataToFile sOut, sFname --> issue

            oCompDef.FlatPattern.ExitEdit
        End If
    Next oComponent

 

    MsgBox "Flat patterns exported to " & SETFilePath, vbInformation, "iLogic"
End Sub

0 Likes
Message 7 of 11

A.Acheson
Mentor
Mentor

From the API help sample for exporting a dxf from a sheetmetal part

 

The below is modified accordingly as well as changing from occurrences to all referenced documents. This will make a more universal rule and not be resticted by assembly hierarchy. This macro is untested.

 

 

Sub ExportFlatPatternToDXF2()

    Dim SETFilePath As String
    SETFilePath = "PATH"
 
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MsgBox "Please open an assembly document.", vbExclamation, "iLogic"
        Exit Sub
    End If

    Dim oAsmDoc As AssemblyDocument
    Set oAsmDoc = ThisApplication.ActiveDocument

    Dim oRefDocs As DocumentsEnumerator = 
    Set oAsmDoc.AllReferencedDocuments
    
    Dim oRefDoc As Document
    For Each oRefDoc In oRefDocs
         
         'Check if it's a Sheetmetal part:
         If oRefDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then      
            Dim oSheetMetDoc As PartDocument 
            Set oSheetMetDoc = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
         
            Dim oCompDef As SheetMetalComponentDefinition 
            Set oCompDef = oSheetMetDoc.ComponentDefinition

            If oCompDef.HasFlatPattern = False Then
                oCompDef.Unfold
            Else
                oCompDef.FlatPattern.Edit
            End If
         
            Dim sOut As String
            sOut = "FLAT PATTERN DXF?AcadVersion=2000&OuterProfileLayer=IV_INTERIOR_PROFILES"

            Dim sFname As String
            sFname = SETFilePath & "\" & oSheetMetDoc.DisplayName & ".dxf"

            ' Get the DataIO object.
            Dim oDataIO As DataIO
            Set oDataIO = oCompDef.DataIO

            oDataIO.WriteDataToFile sOut, sFname

            oCompDef.FlatPattern.ExitEdit
        End If
    Next oRefDoc

    MsgBox "Flat patterns exported to " & SETFilePath, vbInformation, "iLogic"
End Sub

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 8 of 11

mindaV3J2J
Enthusiast
Enthusiast

this got some output!!! However, it failed on the second object when it tried to unfold the second pattern, do you know why this could be? im getting the error message: Mthod 'Unfold' of object 'SheetMetalComponentDefinition' failed

0 Likes
Message 9 of 11

A.Acheson
Mentor
Mentor
Accepted solution

Can the flatpattern be generated manually? If it can then you need to figure out why it did fail. Is the sheetmetal part read only? If it is then no flatpattern can be generated. 

 

As a last resort on error resume next could be used but it will likely skip the operation entirely.

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 10 of 11

Curtis_Waguespack
Consultant
Consultant

@mindaV3J2J How many posts did you make on this topic? Please don't duplicate posts, you ended with multiple people working on the same issue.

 

See my last reply here:

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/turn-off-certain-layers-when-exporti...

 

 

EESignature

0 Likes
Message 11 of 11

mindaV3J2J
Enthusiast
Enthusiast

Apologies, I couldn't find my original post at one point so I made another. Wont happen again I appreciate it.

0 Likes