Scan assembly and all sub-assemblies to export flat patterns

Scan assembly and all sub-assemblies to export flat patterns

blandb
Mentor Mentor
1,851 Views
25 Replies
Message 1 of 26

Scan assembly and all sub-assemblies to export flat patterns

blandb
Mentor
Mentor

Good morning all,

 

Does anyone happen to have a rule that will scan the assembly and all sub-assemblies and or components to export the flat patterns to a specific folder? I believe I have seen where some will do the current level assembly. But didn't know if it was possible to just start at the final assembly and run the rule and it will either generate the flat if it doesn't exists and export the dxf to a specific folder, but if it does exist then just export the dxf to the folder.

 

Also during the export, is there a way to control what is exported in the dxf such as just the outer boundary and bend line only?

 

Only draw back I can see with it auto generating flats would be flat orientation (at an angle when flattened). Obviously it would be better to have users generate their own flats and check that while detailing. So, if that is done, then the exportation of the dxfs, would be no issue.

 

Thanks in advance for any assistance. Have a great week!

Autodesk Certified Professional
0 Likes
Accepted solutions (3)
1,852 Views
25 Replies
Replies (25)
Message 2 of 26

JelteDeJong
Mentor
Mentor
Accepted solution

You could try something like this. The dxf file gets saved in the same directory as the part.

 

    Public Sub main()
        Dim doc As AssemblyDocument = ThisDoc.Document

        For Each refDoc As Document In doc.AllReferencedDocuments

            If (refDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then Continue For
            If (refDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then Continue For

            Dim sheetMetalDoc = ThisApplication.Documents.Open(refDoc.FullFileName)

            Dim fileName = CreateFileName(sheetMetalDoc)
            Export(sheetMetalDoc, fileName)
        Next

        doc.Activate()

    End Sub

    Function CreateFileName(doc As PartDocument) As String
        Dim dirName As String = IO.Path.GetDirectoryName(doc.FullFileName)
        Dim fileNameWithoutExtension As String = IO.Path.GetFileNameWithoutExtension(doc.FullFileName)
        Return String.Format("{0}\{1}.dxf", dirName, fileNameWithoutExtension)
    End Function

    Function Export(doc As PartDocument, newFileName As String)
        Dim oCompDef As SheetMetalComponentDefinition = doc.ComponentDefinition
        If oCompDef.HasFlatPattern = False Then
            oCompDef.Unfold()
        Else
            oCompDef.FlatPattern.Edit()
        End If

        ' more options can be found here:
        ' https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-54B44291-4E1D-4CDB-80CB-FBB38D0239C2
        Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2000& "
        sOut = sOut + "&BendUpLayerColor=255;0;0"
        sOut = sOut + "&BendDownLayerColor=0;255;0"
        sOut = sOut + "&InvisibleLayers=IV_TANGENT"
        oCompDef.DataIO.WriteDataToFile(sOut, newFileName)
        oCompDef.FlatPattern.ExitEdit()
    End Function

 

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 26

blandb
Mentor
Mentor

Thanks for the help. I have been able to change the line type and color, but it appears I cannot change the bend lines to a different layer name, what am I missing here? Also, is there a way for it to place all this into a folder called "DXF's" in the same location as the assembly itself along with not opening every instance of the parts? Maybe running in the background, or if it needs to open, then have it close the files?

 

        ' https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-54B44291-4E1D-4CDB-80CB-FBB38D0239C2
        Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2000& "
	sOut = sOut + "&BendUpLayerColor=255;0;0"
	sOut = sOut + "&BendUpLayerLineType=37637"
        sOut = sOut + "&BendDownLayerColor=255;0;0"
	sOut = sOut + "&BendDownLayerLineType=37637"
        sOut = sOut + "&InvisibleLayers=IV_TANGENT"
        oCompDef.DataIO.WriteDataToFile(sOut, newFileName)

 

 

Autodesk Certified Professional
0 Likes
Message 4 of 26

blandb
Mentor
Mentor

I figured out the naming, I guess I have to add as separate lines? I also removed the last "&" that was after the 2000 in the first line. That seemed to help? I'm still trying to figure out how to place these into a folder.

 

        Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2000"
		sOut = sOut + "&OuterProfileLayer=OBJECT"
		sOut = sOut + "&InteriorProfilesLayer=OBJECT"
		sOut = sOut + "&BendLayer=BEND"
		sOut = sOut + "&BendUpLayerColor=255;0;0"
		sOut = sOut + "&BendUpLayerLineType=37637"
                sOut = sOut + "&BendDownLayerColor=255;0;0"
		sOut = sOut + "&BendDownLayerLineType=37637"
        sOut = sOut + "&InvisibleLayers=IV_TANGENT"
        oCompDef.DataIO.WriteDataToFile(sOut, newFileName)

 

 

Autodesk Certified Professional
0 Likes
Message 5 of 26

jeremy.goenen
Advocate
Advocate

Hi,

change your output setup like this:

in the example below, Layers in the dxf will be "0" and "GRAV"

here is the API link: Inventor 2024 Help | DataIO.WriteDataToFile Method | Autodesk

 

Dim sOut As String

sOut = "FLAT PATTERN DXF?&AcadVersion=2013" _
            + "&InvisibleLayers=IV_ARC_CENTERS;IV_FEATURE_PROFILES_DOWN;IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_FEATURE_PROFILES;IV_ALTREP_FRONT;IV_ALTREP_BACK;IV_ROLL_TANGENT;IV_ROLL" _
            + "&OuterProfileLayer=0&OuterProfileLayerLineType=37633&OuterProfileLayerLineWeight=0.2500&OuterProfileLayerColor=0;0;0" _
            + "&InteriorProfilesLayer=0&InteriorProfilesLayerLineType=37633&InteriorProfilesLayerLineWeight=0.2500&InteriorProfilesLayerColor=0;0;0" _
            + "&UnconsumedSketchesLayer=GRAV&UnconsumedSketchesLayerLineType=37633&UnconsumedSketchesLayerLineWeight=0.2500&UnconsumedSketchesLayerColor=255;255;0"

 

Fanden Sie diesen Beitrag hilfreich? Fühlen Sie sich frei, diesen Beitrag zu liken.
Wurde Ihre Frage erfolgreich beantwortet? Klicken Sie dann auf die Schaltfläche LÖSUNG AKZEPTIEREN

Inventor Versionen:
2025.2
Message 6 of 26

blandb
Mentor
Mentor

Am I correct in placing this "close" operation here (see below in bold). Does this save the file, or simply a close only? Reason I ask is if somone may have made changes in context of the assembly and not open and save the part. Then they run this command, will it capture the changes and save it?

 

Public Sub Main()
        Dim doc As AssemblyDocument = ThisDoc.Document

        For Each refDoc As Document In doc.AllReferencedDocuments

            If (refDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then Continue For
            If (refDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then Continue For

            Dim sheetMetalDoc = ThisApplication.Documents.Open(refDoc.FullFileName)

            Dim fileName = CreateFileName(sheetMetalDoc)
            Export(sheetMetalDoc, fileName)
	    sheetMetalDoc.Close()
        Next

 

Autodesk Certified Professional
0 Likes
Message 7 of 26

jeremy.goenen
Advocate
Advocate

No, it will only close it, see here https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-CF60F6F3-8074-4719-B8E0-5E482E5AF783

Fanden Sie diesen Beitrag hilfreich? Fühlen Sie sich frei, diesen Beitrag zu liken.
Wurde Ihre Frage erfolgreich beantwortet? Klicken Sie dann auf die Schaltfläche LÖSUNG AKZEPTIEREN

Inventor Versionen:
2025.2
0 Likes
Message 8 of 26

jeremy.goenen
Advocate
Advocate

I'm asking just in case, do you also need to add like Partnumber or Revision Number to the DXF for graving with Lasercutting for Example ? If Yes, i can share you an adapted Version of my Code, it is also executed from Assembly or Part Level, like what you try to do.

Fanden Sie diesen Beitrag hilfreich? Fühlen Sie sich frei, diesen Beitrag zu liken.
Wurde Ihre Frage erfolgreich beantwortet? Klicken Sie dann auf die Schaltfläche LÖSUNG AKZEPTIEREN

Inventor Versionen:
2025.2
0 Likes
Message 9 of 26

WCrihfield
Mentor
Mentor

If that document is being referenced by the assembly, it will not actually close anyways.  If opened 'visibly', then closing it will just make it not visible anymore, but it will still remain open until the assembly that is referencing it is closed.  You usually do not need to open each referenced document, because they are already open in the background.  The exception is when all components referencing certain model documents have been suppressed.  In that case, the document they reference can get 'unloaded' from session memory, for performance reasons.  If you opened a document that is not being referenced by the assembly, then closing it would actually close it.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 26

blandb
Mentor
Mentor

@WCrihfield 

 

The original code that was posted was opening each part and the tab would remain open. As you can tell, if there were 50 parts it went through, there were 50 part tabs open. I would then have to go and close them all manually. I was trying to eliminate that and that is why I tried ading the "close" option where I did. It was working as it would open the file, export and then close so the tab. If it could somehow just do that in the back ground and prompt when done, then cool. Also, just trying to capture if anyone was to do "in context" editing, that the dxf would capture that. I would assume it would as if someone was to do in-context editing, then they save the assy, then the file would be saved, so then when this rule is ran, it would be updated. Draw back, is now we are re-creating dxfs. I have another rule where I can do it manually per an instance, so in a rev scenario, I can do that for single parts.

 

Unless anyone has a better approach for how you typically handle these situations. I was just trying to use this as a batch first release type situation. Then handle part specific revisions later in a different format?

 

@jeremy.goenen 

No engraving is needed. As for revisions, I need to see how the guys want to hand that. If we just leave the DXF as the filename.dxf with no indication of revision (this is always the latest and greatest), or if we add an index of revison in the name. I have messed with things like this before, and if the first release is "nothing" then there is always and empy space in the file name, but it works if there is a revision because then there is a character there. Not sure the best way to handle that.

Autodesk Certified Professional
Message 11 of 26

WCrihfield
Mentor
Mentor

If you wanted to keep the Open() method in there, then you could just include the optional second input, which specifies whether to open it visibly or not visibly, and specify False, so they do not open visibly, then you would not have to close any document tabs.  But as mentioned before, opening them is usually not needed.  Each Document object has a ReadOnly Boolean property named Open, which will return True if it is already open, and False if the document has only been 'initialized' (not fully open yet, but will open if anything is done to it).  The very definition of a Document tells us that it represents an 'in-memory' Inventor document.  You can also check if a Document has been modified since it was last saved by checking its Document.Dirty property value.  If True, then it has been modified since it was last saved, and you may want to update/save that document before exporting, depending on your needs.

And if you decide to include Revision Number into the exported file name, you should create a variable to hold the Revision Number value first, then set its value from the iProperty, then check the value of that variable, before trying to include it in the file name.  If it has no value (empty String), then do not include it in the file name.  Same goes for Part Number and other iProperty values.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 26

blandb
Mentor
Mentor

Thanks for the input. I have been messing around trying to place all these DXF's into a DXF folder in the same level as the assembly, but no luck.

Autodesk Certified Professional
0 Likes
Message 13 of 26

WCrihfield
Mentor
Mentor
Accepted solution

Using the example code that @JelteDeJong provided above, I would create a 'shared' variable (declared between custom methods) just for the DXF's folder.  Then set its value within the Sub Main area, while we have a direct reference to the main assembly.  Then add the "\DXF's\" portion to the assembly's path, then make sure that directory exists.  Then down within the CreateFileName Function, just use that variable when specifying the new full file name of the DXF file.  Below is a modified version of that earlier code, with these changes.

 

Public Sub Main()
    Dim doc As AssemblyDocument = ThisDoc.Document
	sAsmDXFFolder = System.IO.Path.GetDirectoryName(doc.FullFileName)
	sAsmDXFFolder = sAsmDXFFolder & "\DXF's"
	If System.IO.Directory.Exists(sAsmDXFFolder) = False Then
		System.IO.Directory.CreateDirectory(sAsmDXFFolder)
	End If
    For Each refDoc As Document In doc.AllReferencedDocuments
        If (refDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then Continue For
        If (refDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then Continue For
        Dim fileName = CreateFileName(refDoc)
        Export(refDoc, fileName)
    Next
    doc.Activate()
End Sub

Dim sAsmDXFFolder As String

Function CreateFileName(doc As PartDocument) As String
    Dim fileNameWithoutExtension As String = IO.Path.GetFileNameWithoutExtension(doc.FullFileName)
    Return String.Format("{0}\{1}.dxf", sAsmDXFFolder, fileNameWithoutExtension)
End Function

Function Export(doc As PartDocument, newFileName As String)
    Dim oCompDef As SheetMetalComponentDefinition = doc.ComponentDefinition
    If oCompDef.HasFlatPattern = False Then
        oCompDef.Unfold()
    Else
        oCompDef.FlatPattern.Edit()
    End If
    ' more options can be found here:
    ' https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-54B44291-4E1D-4CDB-80CB-FBB38D0239C2
    Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2000& "
    sOut = sOut + "&BendUpLayerColor=255;0;0"
    sOut = sOut + "&BendDownLayerColor=0;255;0"
    sOut = sOut + "&InvisibleLayers=IV_TANGENT"
    oCompDef.DataIO.WriteDataToFile(sOut, newFileName)
    oCompDef.FlatPattern.ExitEdit()
End Function

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 14 of 26

blandb
Mentor
Mentor

@WCrihfield .

Thanks for the sample.

Autodesk Certified Professional
0 Likes
Message 15 of 26

jeremy.goenen
Advocate
Advocate

Just to give an idea, if you set up your filename like this, there will not be an empty space in case of a "Null" Revision:

 

'get the filename
filename = ThisDoc.FileName(False) 'without extension

'get revision indicator
Dim revindicator As String
Try	
	If Not (iProperties.Value("Project", "Revision Number") = "") Then 'you can use what you want here
	revindicator = "-REV" & iProperties.Value("Project", "Revision Number") 
	Else
	revindicator = ""
	End If
Catch
revindicator = ""
End Try
	
'set the dxf filename
Dim dxffilename As String
dxffilename = filename & revindicator & ".dxf"	

Fanden Sie diesen Beitrag hilfreich? Fühlen Sie sich frei, diesen Beitrag zu liken.
Wurde Ihre Frage erfolgreich beantwortet? Klicken Sie dann auf die Schaltfläche LÖSUNG AKZEPTIEREN

Inventor Versionen:
2025.2
0 Likes
Message 16 of 26

blandb
Mentor
Mentor

Interesting....All was ok yesterday, but I went to run this today after closing down and restarting today, and now I get the following message. Only thing I did differently was installed Nesting 2024 to play around with it.

blandb_0-1702487850164.pngblandb_1-1702487910716.png

 

Autodesk Certified Professional
0 Likes
Message 17 of 26

WCrihfield
Mentor
Mentor

Hi @blandb.  I am not 100% sure what caused that error, but what comes to mind is that the main assembly is the 'active' document, but then you are 'activating' the 'flat pattern edit mode' of one of the parts within the assembly, without first entering into 'component edit mode' for the component, and maybe that is where the problem originates from.  In my opinion, you do not even need to enter into the 'edit mode' of the flat pattern.  As long as the flat pattern exists, or it can be created using the 'unfold' method, then all you need to do is use the FlatPattern.DataIO instead of the SheetMetalComponentDefinition.DataIO object, without being in edit mode.  That edit mode is just for user interactions.

Edit:  Possibly, when you were opening them visibly, it was not a problem, but now that you are leaving them not visible, it does not like you trying to activate the edit mode on the flat pattern in a referenced part.  Just my thoughts/suspicions.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 18 of 26

blandb
Mentor
Mentor

Weird thing is, I am running this in the same assy I was yesterday. I just created a new rule and posted the original code from @JelteDeJongand it worked. Then, I ran the failing rule again which was the last code from @WCrihfield where it was placing all into a specific folder (which worked yesterday), and mysteriously it worked....hmmmm

Autodesk Certified Professional
0 Likes
Message 19 of 26

blandb
Mentor
Mentor

@WCrihfield 

Unfortunatley you are speaking above my head lol. I'm not certain what to change based on what you are saying?

Autodesk Certified Professional
0 Likes
Message 20 of 26

jeremy.goenen
Advocate
Advocate
Accepted solution

Hello @blandb, based on the Screenshot you shared, your Code will Run fine aslong as the target part has no flatpattern, because you can use the unfold method on a "in memory" only Document, the flatpattern.edit can only be used on Visible Acitive Documents, which will cause an exception in your case, because you dont Open the Document first. To export your dxf the way you want, you dont need to enter edit mode, so if you remove this Line you should be fine.

Fanden Sie diesen Beitrag hilfreich? Fühlen Sie sich frei, diesen Beitrag zu liken.
Wurde Ihre Frage erfolgreich beantwortet? Klicken Sie dann auf die Schaltfläche LÖSUNG AKZEPTIEREN

Inventor Versionen:
2025.2