iLogic to open IDW and change model state from assembly

iLogic to open IDW and change model state from assembly

dschulteHR4D5
Advocate Advocate
433 Views
6 Replies
Message 1 of 7

iLogic to open IDW and change model state from assembly

dschulteHR4D5
Advocate
Advocate

Hello, I have some various codes that i use to export documents one of them using component occurrences to find a parts IDW, open it and print that IDW. 

 

My question is, is there a way to use the component occurrences of the assembly to find the reference document (IDW), see the model state within the current assembly, open that IDW, and then change the model state within the IDW to the model state that is being referenced within the assembly.

 

I already have all of the code written and working to open the idws and do the exports, just looking to see if it can see the model state and change it within the drawing.

0 Likes
434 Views
6 Replies
Replies (6)
Message 2 of 7

Pineapple2024
Advocate
Advocate

This change ModelState for each view:

 

			If oDrawingView.ActiveModelState Is Nothing Then
				Logger.Info("No Modelstate")
				'Logger.Info(oDrawingView.ReferencedDocumentDescriptor.FullDocumentName)
				
			ElseIf oDrawingView.ActiveModelState <> oMSName Then
				Logger.Info(oDrawingView.Label.Text & " " & oDrawingView.ActiveModelState)
				oDrawingView.SetActiveModelState(oMSName, True, True)
			End If

 

 

Changing ModelState to same one cause error, hence the name check first.

I failed to get part/assembly ModelStates in the drawing.

If you start from assembly:

	oAssemDoc = TryCast(oDoc, AssemblyDocument)
	Logger.Info("ModelState Assembly Drawing Update " & oDoc.DisplayName)
	oAssemCompDef = oAssemDoc.ComponentDefinition
	If oAssemCompDef.IsModelStateFactory Then
		Logger.Info("ModelState in Assembly")
		oModelStates = oAssemCompDef.ModelStates
	End If
0 Likes
Message 3 of 7

jnowel
Advocate
Advocate

If you only need to change model state in the drawing, maybe something like this.
it changes the all the model states to "Primary" for all drawing views in the IDW.
Adjust to suit your needs.

Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
For Each oSheet As Sheet In oDoc.Sheets
	For Each oView As DrawingView In oSheet.DrawingViews
		'Show Current model state rep
		Logger.Info(oView.ActiveLevelOfDetailRepresentation)
		 'set to [Primary] model state
		 oView.SetActiveModelState("[Primary]", True)
		 'Show changed model state rep
		 Logger.Info(oView.ActiveLevelOfDetailRepresentation)
	Next
Next

 

0 Likes
Message 4 of 7

dschulteHR4D5
Advocate
Advocate

So have been back and forth with other work so havent had alot of time to play with this, but have gotten it to open a drawing and set it to the primary model state. Which i could have some use for, but mainly what i am trying to do is from an assembly document reference the parts model state (not primary) within the assembly, open the IDW, and set that MS of the IDW to the MS of the part if it is possible. 

 

I have the codes to open the IDWs from the assembly using ReferencedDocumentDescriptor.ReferencedDocument.

0 Likes
Message 5 of 7

WCrihfield
Mentor
Mentor

Hi @dschulteHR4D5.  What you said in your last reply is still not very clear.

"what i am trying to do is ":

  • "from an assembly document "
    • Based on that phrase, I assume that the iLogic rule you are requesting help with will be ran while an assembly is the 'active' document.
  • "reference the parts model state (not primary) within the assembly"
    • What do you mean by "the parts"...do you mean every part only, and none of the sub assemblies?..., or do you mean one specific part?
    • Which ModelState?...do you mean the ModelState that the ComponentOccurrence object is set to?
  • " open the IDW"
    • What IDW?  The one for the 'main' assembly, or one that is just for that one part / sub assembly that the ComponentOccurrence is referencing?
  • "and set that MS of the IDW to the MS of the part if it is possible. "
    • ???  An IDW does not have a ModelState.  An individual DrawingView within an IDW can be 'set to' reference a specific ModelState version of a model document though.
    • Oddly stated, so difficult to understand.  Do you want to change the ModelState of the ComponentOccurrence in the assembly to match the ModelState that a specific DrawingView in the IDW is set to, without changing the ModelState of the DrawingView?
    • Or...do you want to change which ModelState a specific DrawingView in the IDW is set to, based on which ModelState that a specific ComponentOccurrence is set to?
      • If so, which specific ComponentOccurrence, and which specific DrawingView?  There may be multiple instances / occurrences in an assembly that all reference the same exact model document.  And a drawing can contain different views for different ModelState versions of the same model document, or it can contain views of multiple different model documents.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 7

dschulteHR4D5
Advocate
Advocate

Sorry for not being very clear, I guess the general scope of the rule would be run from an assembly, the assembly would not have any sub assemblies only parts. These parts contained in this assembly are being sent out to different companies to be made. Essentially each part that is being sent out to company A, would be flame cut so they are only looking at the IDW of the part with the clearance holes/profile to cut, then company B would get the full finished print, showing all the holes, clearance holes/profile and tapped holes and other smaller holes that company A would not flame cut. 

 

So I have been making a print of the finished part-which company B would get, but without making a separate print for each part or making separate parts and prints I have been using model state within each part to show only the flame cut holes and profile. So within each parts drawing i make the finished drawing, then set the model state of the base view to show only what company A needs then export to send out then swap back to the primary model state to send out the print to company B.

 

Basically my thought was to would take each part that needs this done, drop it into an assembly with no sub assemblies, set the model state of each part within this assembly to "company A" that way the rule could look at the model state of the part within the assembly, open the parts idw-already finished for company B, then change the view within the idw to "company A" to run the export rules on the idw. 

 

Thinking about it now, I name the model state the same for each part that this is done for, it might make more sense to have the rule after it opens the IDW, to just change the model state of the view to "company B" ? Instead of having to reference the model state of the part, as this rule would basically only be doing this anyways then running the export rules on the IDW, it wouldn't need to check every parts model state, just know to change the model state of the drawing view within the drawing, given that the part has the model state already saved in the part. 

 

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

Hi @dschulteHR4D5.  I'm on my way out.  I'm not sure if I'm in the ball park with this code example, but its what I could throw together in a hurry, based on most of what you said in your last reply.  It is designed to start from an assembly.  As it iterates components, it assumes every one is a part, and tries to find the drawing for it.  If found, it tries to export the 'B' version.  I was not sure what you named your ModelStates, or how you are exporting your drawings, so I am just showing SaveAs lines right now.  It then attempts to change all views to the A version ModelState, then exports the A version of the drawing.  Then closes it, without saving, and moves on.  Trying not to process the same drawing more than once.

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oADoc As AssemblyDocument = TryCast(ThisDoc.Document, Inventor.AssemblyDocument)
	If oADoc Is Nothing Then Return
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oADef.Occurrences
	Dim oProcessedFFNs As New List(Of String)
	Dim oFMgr As FileManager = oInvApp.FileManager
	Dim oDocs As Inventor.Documents = oInvApp.Documents
	For Each oOcc As ComponentOccurrence In oOccs
		Dim sRef_FDN As String = oOcc.ReferencedDocumentDescriptor.FullDocumentName
		Dim sRef_FFN As String = oFMgr.GetFullFileName(sOccRef_FDN)
		If oProcessedFFNs.Contains(sRef_FFN) Then Continue For
		Dim sIDW As String = System.IO.Path.ChangeExtension(sRef_FFN, ".idw")
		If System.IO.File.Exists(sIDW) Then
			Dim oIDW As DrawingDocument = oDocs.Open(sIDW, False)
			Dim sPDF_Path As String = System.IO.Path.GetDirectoryName(sIDW)
			Dim sFName As String = System.IO.Path.GetFileNameWithoutExtension(sIDW)
			Dim sPDF_A As String = System.IO.Path.Combine(sPDF_Path, sFName & " A.pdf")
			Dim sPDF_B As String = System.IO.Path.Combine(sPDF_Path, sFName & " B.pdf")
			'<<< export B version >>>
			oIDW.SaveAs(sPDF_B, True)
			'<<< change ModelState of every view on every sheet >>>
			For Each oSheet As Inventor.Sheet In oIDW.Sheets
				For Each oView As DrawingView In oSheet.DrawingViews
					If oView.ActiveModelState IsNot Nothing AndAlso
						oView.ActiveModelState <> "A" Then
						Try
							oView.SetActiveModelState("A")', True, True)
						Catch
							'what to do if that fails
						End Try
					End If
				Next oView
			Next oSheet
			'<<< export A version >>>
			oIDW.SaveAs(sPDF_A, True)
			oIDW.ReleaseReference
			oDocs.CloseAll(True) 'clears out all unreferenced documents
			oProcessedFFNs.Add(sRef_FFN)
		End If
	Next oOcc
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes