How to export primary model state flat pattern of a sheetmetal

How to export primary model state flat pattern of a sheetmetal

e_jeanUH6EY
Explorer Explorer
604 Views
7 Replies
Message 1 of 8

How to export primary model state flat pattern of a sheetmetal

e_jeanUH6EY
Explorer
Explorer

Hi everyone, 
First time posting, we have an iLogic rule to traverse an assembly and export all the individual sheet metal flat patterns to each individual .dwg file. It worked well but now we're startint to use model states into sheet metal parts as the "Primary" model state will always be the flat pattern we want to export. But the rule exports the active model state which is not always the "Primary". 
How to make the rule export the "Primary" model state?
Thanks for any help!

0 Likes
Accepted solutions (1)
605 Views
7 Replies
Replies (7)
Message 2 of 8

Pineapple2024
Advocate
Advocate

Set Active ModelState to first ModelState.

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-ModelStates

oDoc = ThisApplication.ActiveDocument
Logger.Info(oDoc.DisplayName)
oCompDef = oDoc.ComponentDefinition
If oCompDef.IsModelStateFactory Then
	Logger.Info("ModelState in file")
	oModelStates = oCompDef.ModelStates
	Logger.Info(oModelStates.Item(1).Name)
	oModelStates.Item(1).Activate
End if
Message 3 of 8

WCrihfield
Mentor
Mentor

Hi @e_jeanUH6EY.  Your attached code is very difficult for me to read through and follow along with, and looks like it may have been VBA at some point, then converted to vb.net later.  I can see that down where you are exporting to a DWG file, you appear to be getting the DataIO object from a variable named 'docdefinition', which I believe represents some sort of ComponentDefinition.  Since this is likely a definition of some reference document, instead of the definition of the main assembly, I am not sure if simply activating the first ModelState found within that definition will be enough to make that export line of code work the way you want.  If you had the part open directly, and visibly on your screen, then activating the first ModelState before doing anything else would work, but that is not the case in your code, which is being ran from your main assembly.  This is because of how you obtained the value of that variable (docdefinition) in the first place.  You would need to first make sure you have a reference to the Document object associated with the first ModelState in that file, then get the ComponentDefinition from that Document, as the value of that variable.  That way that variable represents the ComponentDefinition of the Primary/Master Document version of that file, which is what you want to export from. 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 8

e_jeanUH6EY
Explorer
Explorer

 

Thanks for your reply WCrihfield  
When you say "You would need to first make sure you have a reference to the Document object associated with the first ModelState in that file, then get the ComponentDefinition from that Document, as the value of that variable."
do you have a code example i could use as starting point?

 

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor

Hi @e_jeanUH6EY.

This is one of those situations that almost needs a university course paper to properly explain or describe.  The ModelState object has two Properties for Document Type objects (ModelState.Document & ModelState.FactoryDocument), and both are ReadOnly, which just means you only 'get' their value, but can not 'set' another Document as their value.  The Property named "FactoryDocument" is supposed to return a Document object that is associated with the 'active' ModelState in that File.  However, if accessing that Property of the 'active' ModelState, it will not have a value (it will be Nothing).  A 'File' will only have a ModelState 'factory' Document, if it has more than just the one, original ModelState in it.  If a File has a 'factory' Document, then that is the only one you can make direct changes to, and is the one you must access the other ModelStates related resources through, to make changes to them.  The ModelState Property simply named "Document" is supposed to return the 'member' Document that is associated with that ModelState.  However, this property will also not have a value (will be Nothing), if that ModelState has never been referenced by an assembly component occurrence before.  When you place an assembly component occurrence into an assembly, set to a specific custom ModelState version of the File it references, that action automatically generates the 'member' Document for that ModelState, if it did not already exist, and may update the existing member Document, if it already existed.  But we usually can not directly edit a ModelState 'member' Document (but can read data from it, if it exists).

 

So, if we need to make changes to a ModelState member Document, we must do one of the following things:

  • If working with a part or assembly directly, while it is the 'active' Document (visibly showing on your screen at that moment), then:
    • You can simply activate the ModelState that you want to make changes to
    • Then make sure you have a reference to the Document associated with that newly active ModelState
    • Then make those changes to that Document
  • If working working with a part or assembly remotely (a referenced Document in the background, not the 'active' one), then:
    • We need to find out if that File has multiple ModelStates in it (or has a 'factory'), and if so, get a reference to that factory document first.
    • If you need to 'create' any new UserParameters, or custom iProperties, or new features, you can do so within this factory document, and they will also exist for all other ModelState versions, either with the same initial value, or no value (a parameter must have a value, but a property can have an empty value).  A new feature may be suppressed in all other ModelStates.
    • If you want to change values of existing parameters, properties, or suppression statuses, then you can do that within the ModelStateTable, which is accessed from the ModelStates.ModelStateTable property.  But that table will only exist if there is a ModelState factory, and you are accessing that property from through factory document.  The ModelStateTable can be navigated by its rows & columns.  Each row represents a ModelState member, and each column represents something like a parameter, iProperty, suppression status of a component occurrence, suppression status of a feature, or something like that.  Each column will have a heading indicating what that column is for.  The cell at the intersection of a row and column will have a String type value that is Read/Write, which you can change the value of.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8

e_jeanUH6EY
Explorer
Explorer

Thank you for the detailed explanation. Our situation is actually more straightforward:

  1. In our sheet metal parts, the 'Primary' model state always contains the flat pattern representing the raw plate for CNC cutting.
  2. In the assembly, sheet metal parts are shown in their finished state (after cutting, drilling, and milling) a different model state.
  3. When our iLogic rule traverses the assembly, we need to export the flat pattern to a DWG file from the 'Primary' model state of each sheet metal part, not the currently active model state that the part is in the assembly. I'd like to clarify that we don't need to make any changes to the sheet metal parts themselves. The 'Primary' model state with the desired flat pattern already exists in each part. We simply need a way to access and export this specific model state's flat pattern, even when a different state is active in the assembly. Our goal is to retrieve existing information from a non-active model state, not to modify the parts or create new data.

I hope this helps make it clearer and easier to find a solution.

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor
Accepted solution

OK.  Then, are you saying that you are not exporting a 'true' FlatPattern object, but just a flat part from the 'folded model' mode, that simply does not have any bends or flanges in it yet?  It took me a while to create an alternate code example that you can review, but when I typed this up, I had a regular FlatPattern object in mind, not just a regular part with no bends or folds in it yet.

Sub Main
	oInvApp = ThisApplication
	sPrimaryMSName = oInvApp.LanguageTools.CurrentPrimaryModelStateString
	Dim oDoc As Inventor.Document = ThisDoc.Document
	Dim sMainDWG As String = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".dwg")
	ExportFlatPatternToDWG(oDoc, sMainDWG)
	For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
		Dim sRefDWG As String = System.IO.Path.ChangeExtension(oRefDoc.FullFileName, ".dwg")
		ExportFlatPatternToDWG(oRefDoc, sRefDWG)
	Next oRefDoc
End Sub

Dim oInvApp As Inventor.Application
Dim sPrimaryMSName As String

Sub ExportFlatPatternToDWG(oDoc As Inventor.Document, sDWG_FullFileName As String)
	If (oDoc Is Nothing) OrElse (Not TypeOf oDoc Is PartDocument) Then Return
	Dim oPDoc As PartDocument = oDoc
	If (Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition) Then Return
	Dim sMS_Name As String = oPDoc.ModelStateName
	If (Not String.IsNullOrEmpty(sMS_Name)) AndAlso (sMS_Name <> sPrimaryMSName) Then
		Dim sFDN As String = oInvApp.FileManager.GetFullDocumentName(oPDoc.FullFileName, sPrimaryMSName)
		oPDoc = oInvApp.Documents.Open(sFDN, False)
	End If
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then
		oFP = oSMDef.FlatPattern
	Else
		Try
			oSMDef.Unfold
			oFP = oSMDef.FlatPattern
			oFP.ExitEdit
		Catch
		End Try
	End If
	If oFP Is Nothing Then Return
	'<<< CHANGE THIS FORMAT STRING AS NEEDED >>>
	Dim sFormat As String = "FLAT PATTERN DWG?AcadVersion=2004&Outerprofilelayer=0&Interiorprofileslayer=0&BendLayer=6&BendDownLayer=6&InvisibleLayers=IV_TANGENT;IV_ARC_CENTERS;IV_FEATURE_PROFILES;IV_BEND;IV_BEND_DOWN;IV_FEATURE_PROFILES_DOWN&MergeProfilesIntoPolyline=TRUE&SimplifySplines=TRUE&REBASEGEOMETRY=TRUE"
	'&BendLayer=SCRIBE&BendDownLayer=SCRIBE
	Try
		oFP.DataIO.WriteDataToFile(sFormat, sDWG_FullFileName)
		Logger.Error("Following DWG Was Created:" & vbCrLf & oPDoc.sDWG_FullFileName)
	Catch
		Logger.Error("Error Exporting Following Part To DWG!" _
		& vbCrLf & oPDoc.FullDocumentName)
	End Try
End Sub

But if we are just exporting the regular part, we can use a slightly simpler version of practically the same code.

Sub Main
	oInvApp = ThisApplication
	sPrimaryMSName = oInvApp.LanguageTools.CurrentPrimaryModelStateString
	Dim oDoc As Inventor.Document = ThisDoc.Document
	Dim sMainDWG As String = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".dwg")
	ExportFlatPatternToDWG(oDoc, sMainDWG)
	For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
		Dim sRefDWG As String = System.IO.Path.ChangeExtension(oRefDoc.FullFileName, ".dwg")
		ExportFlatPatternToDWG(oRefDoc, sRefDWG)
	Next oRefDoc
End Sub

Dim oInvApp As Inventor.Application
Dim sPrimaryMSName As String

Sub ExportFlatPatternToDWG(oDoc As Inventor.Document, sDWG_FullFileName As String)
	If (oDoc Is Nothing) OrElse (Not TypeOf oDoc Is PartDocument) Then Return
	Dim oPDoc As PartDocument = oDoc
	If (Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition) Then Return
	Dim sMS_Name As String = oPDoc.ModelStateName
	If (Not String.IsNullOrEmpty(sMS_Name)) AndAlso (sMS_Name <> sPrimaryMSName) Then
		Dim sFDN As String = oInvApp.FileManager.GetFullDocumentName(oPDoc.FullFileName, sPrimaryMSName)
		oPDoc = oInvApp.Documents.Open(sFDN, False)
	End If
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	'<<< CHANGE THIS FORMAT STRING AS NEEDED >>>
	Dim sFormat As String = "FLAT PATTERN DWG?AcadVersion=2004&Outerprofilelayer=0&Interiorprofileslayer=0&BendLayer=6&BendDownLayer=6&InvisibleLayers=IV_TANGENT;IV_ARC_CENTERS;IV_FEATURE_PROFILES;IV_BEND;IV_BEND_DOWN;IV_FEATURE_PROFILES_DOWN&MergeProfilesIntoPolyline=TRUE&SimplifySplines=TRUE&REBASEGEOMETRY=TRUE"
	'&BendLayer=SCRIBE&BendDownLayer=SCRIBE
	Try
		oSMDef.DataIO.WriteDataToFile(sFormat, sDWG_FullFileName)
		Logger.Error("Following DWG Was Created:" & vbCrLf & oPDoc.sDWG_FullFileName)
	Catch
		Logger.Error("Error Exporting Following Part To DWG!" _
		& vbCrLf & oPDoc.FullDocumentName)
	End Try
End Sub

Both of these examples can be ran from any type of document.  If the current document is a sheet metal part, it will attempt to export it to DWG.  If it is an assembly or drawing, those usually have referenced documents, and it will iterate through those, and attempt to export any sheet metal parts it finds to DWG.  I copied the 'Format' String from your attached code, so I hope that is OK.  It is just saving those DWG files in the same location as the part file, and with the same name as the part file, so you may have to change that, if needed.  But this is just example code for review and testing.  You can change it however you want, or copy parts of it out into your other code, if you want.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 8

e_jeanUH6EY
Explorer
Explorer

I just did a quick test with your first example, and it works perfectly. I will test it more thoroughly for different situations but it seems very promising. Once I have run a couple of tests, I will mark this as Solution Accepted.
Again thank you so much for taking the time to help us.

0 Likes