Automating DXF Exports: Handling Model States in Sheet Metal Assemblies

Automating DXF Exports: Handling Model States in Sheet Metal Assemblies

Nedeias
Participant Participant
371 Views
3 Replies
Message 1 of 4

Automating DXF Exports: Handling Model States in Sheet Metal Assemblies

Nedeias
Participant
Participant

Hello Forum,

I've been struggling all day with this issue, and I’m hoping someone can help. I have an assembly that includes various parts, some of which are configured with multiple model states. My goal is to generate DXF files for all sheet metal parts in the assembly, but I keep running into a problem: every time I try to export a DXF, the file opens in the primary model state, giving me the wrong flat pattern.

I’ve been reading through forums and trying different solutions, including ChatGPT, but no luck so far. It seems like it should be simple to open a part in its active model state and generate the flat pattern from there, but I haven’t been able to get it to work.

Has anyone encountered this issue before? Any help or code tips would be greatly appreciated!

Here is a sample code I found in the forum and attempted to modify to suit my needs.

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument

'Check that the active document is an assembly file
If oDoc.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
	Exit Sub
End If

'Get user input
RUsure = MessageBox.Show ( _
"This will create a DXF file for all of the asembly components that are sheet metal." _
& vbLf & "This rule expects that the part file is saved." _
& vbLf & " " _
& vbLf & "Are you sure you want to create DXF for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic  - Batch Output DXFs ", MessageBoxButtons.YesNo)

If RUsure <> vbYes Then Exit Sub

Dim oAsmName As String = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 4)
Dim oPath As String = ThisDoc.Path

'Get the DXF target folder path
Dim oFolder As String = oPath & "\" & oAsmName & " DXF Files"

'Check for the DXF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If

'- - - - - - - - - - - - -Component  - - - - - - - - - - - -
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Dim oBOM As BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
oBOM = oDoc.ComponentDefinition.BOM
'MessageBox.Show("Test", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)

oBOM.StructuredViewEnabled = True
Dim oBOMView As BOMView = oBOM.BOMViews.Item(oBOM.BOMViews.Count)
For Each oRow As BOMRow In oBOMView.BOMRows
Try
	Dim oCD As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
	Dim iDoc As Document = oCD.Document
	'SheetMetal parts only
	If iDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
	Dim iName As String = iDoc.FullFileName

	'Check that model is saved
	If iName = vbNullString Then Continue For
	iDoc = ThisApplication.Documents.Open(iName)
	oCD = iDoc.ComponentDefinition
	
	Dim oItem As String = oRow.ItemNumber
	Try
		If Not oCD.HasFlatPattern Then
			oCD.Unfold()
		Else
			oCD.FlatPattern.Edit()
		End If
		Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=2007&RebaseGeometry=False" _
							+ "&OuterProfileLayer=0&OuterProfileLayerColor=0;0;0" _
							+ "&InteriorProfilesLayer=0&InteriorProfilesLayerColor=0;0;0" _
							+ "&BendUpLayer=G&BendUpLayerColor=255;255;0" _
							+ "&BendDownLayer=G&BendDownLayerColor=255;255;0" _
							+ "&SimplifySplines=True&MergeProfilesIntoPolyline=True" _
							+ "&InvisibleLayers=IV_ARC_CENTERS;IV_TANGENT;IV_ROLL;IV_ROLL_TANGENT;IV_ALTREP_BACK;IV_ALTREP_FRONT;IV_FEATURE_PROFILES_DOWN;IV_FEATURE_PROFILES;IV_TOOL_CENTER_DOWN;DIGI_MARKER_TOOL_1;DIGI_MARKER_TOOL_2"
		oCD.DataIO.WriteDataToFile(sOut, oFolder & "\" & oAsmName & "-" & oItem & ".dxf")
		oCD.FlatPattern.ExitEdit()
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
	iDoc.Close(True)
Catch
End Try
Next




0 Likes
Accepted solutions (2)
372 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Nedeias.  It looks like you may need to learn the difference between FullFileName and FullDocumentName for this type of situation.  The FullFileName is strictly just what you would expect (path, file name, & file extension), and is used for actual file specification.  The FullDocumentName will start with the FullFileName, but then sometimes will include extra information immediately after the file extension, which specific version of the Document associated with that File you want to be working with.  It is mostly used in iParts/iAssemblies and now in ModelStates.  When you have multiple ModelStates in a single File on disk, then you can have multiple Documents in that one File also.  The Document object is just organized data being held in Inventor's session memory, while the File is what is stored on the hard drive.  For example, a Document can exist without a File, if it has not been saved yet.  And a Document can be different than what is stored in the File associated with it, if you have made changes to the Document, but have not saved them yet.

 

Where you have these lines of code:

 

Dim iName As String = iDoc.FullFileName

 

and

 

iDoc = ThisApplication.Documents.Open(iName)

 

...you should be using FullDocumentName there instead.  That will preserve the ModelState specification of the document being referenced by that row of the BOM.  There may be other needs in this situation also, but that is the first one I came accross.

 

Edit:  Also, you do not actually need to enter into 'Edit Mode' of the FlatPattern before you export it to DXF.  The only thing that may help with is making sure it is updated, but you can simply update the part before you get to that point.  You can just get the FlatPattern object itself, which is a type of ComponentDefinition of its own, then use the DataIO of that object for the export (FlatPattern.DataIO.WriteDataToFile()).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Also, your document type check is too late.  Since you already declared your primary document variable as being AssemblyDocument, then already set its value with ThisApplication.ActiveDocument, it will already have thrown an error by that point if a different document type was active, so it would not even make it to your document type check.  There are lots of ways to work around that specific situation though.  You could check the ThisApplication.ActiveDocumentType property first, before actually getting the Document object.  Or you could not declare its type, then get it, then check its type, then if it's OK, set it to another variable that was declared for that type of object.  But that one is more lengthy.  One of the processes I like recently is using 'TryCast' to try to case the Document I obtain to the type of document I want.  If it fails, it will not throw an error, but will simply not assign a value to that variable.  Then check to see if your variable got a value assigned to it in the next line, like this:

Dim oDoc As AssemblyDocument = TryCast(ThisApplication.ActiveDocument, AssemblyDocument)
If oDoc Is Nothing Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
	Exit Sub
End If

That one alternative uses this check:

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then

or

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then

...depending on which code property you use to obtain the document.  Whichever one you use, stick with it for the rest of the code, instead of mixing them up.  Its just safer that way, because ThisApplication.ActiveDocument and ThisDoc.Document can point to different documents in some situations.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 4

Nedeias
Participant
Participant

Thank you so much @WCrihfield !!!! Have a great day.

0 Likes