Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

iLogic include work axis of a part in the assembly view in a drawing

TecnicoPanni
Participant

iLogic include work axis of a part in the assembly view in a drawing

TecnicoPanni
Participant
Participant

Hello
I'm finding this rule very useful, written by @JhoelForshav, in order to include work axis from assembly to drawing.

 

Dim View As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Pick view")
Dim refDoc = View.ReferencedDocumentDescriptor.ReferencedDocument
Dim oAxes As WorkAxes = refDoc.ComponentDefinition.WorkAxes
For Each oAxis As WorkAxis In oAxes
Dim oInclude As DialogResult
oInclude = MessageBox.Show("Include " & oAxis.Name & "?", "Include Axis", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If oInclude = DialogResult.Yes Then
View.SetIncludeStatus(oAxis, True)
Else
View.SetIncludeStatus(oAxis, False)
End If
Next


Sometime I'd like to include the work axis of a part in the assembly view, not of the assembly itself.

After selecting the view, is there a way to select the part I like to in the selected view?


Thank you

0 Likes
Reply
577 Views
13 Replies
Replies (13)

WCrihfield
Mentor
Mentor

Hi @TecnicoPanni.  There is a way to select just a piece of the model geometry within a view, then through the use of a lot of complex code, get a reference to the real model edge, in the context of the model file, but I do not recommend that route.  Since you are already planning on a lot of manual user interaction (picking a view with your mouse, selecting a part from a list of parts, then selecting or verifying the name of each work feature), I would simply suggest that you do this the somewhat manual way.  To do this, simply expand the BrowserNode for the drawing view within the Model Browser tree, then expand the node next to the assembly, then expand the node next to the part, then expand the node next to its Origin folder, then right-click on the WorkAxis you want to include, and choose 'Include' from that right-click menu.

WCrihfield_0-1675948153569.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

TecnicoPanni
Participant
Participant

Hi @WCrihfield ,

first of all, thank you for the time you're dedicating me!

I already have been doing manually as you suggested.
Actually, I'm only interested on the importing the main workaxis "X Axis" or "Y Axis" or "Z Axis", as I maily work with cilindrical parts (only one axis of revolution, which is not in common with the assembly axis).

Indeed I already modify the rule in this way.

Dim View As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Scegli una vista")
Dim refDoc = View.ReferencedDocumentDescriptor.ReferencedDocument
Dim oAxes As WorkAxes = refDoc.ComponentDefinition.WorkAxes
For Each oAxis As WorkAxis In oAxes
	Dim oInclude As DialogResult
	If oAxis.Name = "Asse Z" or oAxis.Name = "Asse X" or oAxis.Name = "Asse Y" Then
		GoTo si
	Else
		GoTo no
	End If
	si:
	oInclude = MessageBox.Show("Includere " & oAxis.Name & "?", "Includere Asse", _
	MessageBoxButtons.YesNo, MessageBoxIcon.Question)
	If oInclude = DialogResult.Yes Then
		View.SetIncludeStatus(oAxis, True)
		'oAxis.Visible = True
		'oAxis.AutoResize = True
	Else
		View.SetIncludeStatus(oAxis, False)
		
		no:
	End If
Next

I was thinking at a rule that make me:

-select a view of the assembly

-select a part from a list made from the only FileName (I'm dealing with max 10 parts in an assembly) (this should be the only added step, if comparing the current rule)

-Choise (Yes/No) if import the workaxis "X Axis" or "Y Axis" or "Z Axis"

0 Likes

WCrihfield
Mentor
Mentor

OK @TecnicoPanni .  Here is something I just threw together for you to try out.  Look this over, and/or test it out, and let me know if this will work OK for you.

Sub Main
	Dim View As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Scegli una vista")
	If IsNothing(View) Then Exit Sub
	Dim ViewDoc As Document = View.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oMainAxes As WorkAxes = ViewDoc.ComponentDefinition.WorkAxes
	For Each Axis As WorkAxis In oMainAxes
		ChooseIncludeInView(View, Axis)
	Next
	Dim oDocNames As New List(Of String)
	For Each oRefDoc As Document In ViewDoc.AllReferencedDocuments
		oDocNames.Add(oRefDoc.FullDocumentName)
	Next
	Dim oDocName As String = InputListBox("Choose Sub Doc To Include Axis From", oDocNames, "")
	If oDocName = "" Then Exit Sub
	Dim oSubDoc As Document = ThisApplication.Documents.ItemByName(oDocName)
	Dim oSubAxes As WorkAxes = oSubDoc.ComponentDefinition.WorkAxes
	For Each oSubAxis As WorkAxis In oSubAxes
		ChooseIncludeInView(View, oSubAxis)
	Next
	'View.Parent.Update 'update the sheet
End Sub

Sub ChooseIncludeInView(oView As DrawingView, oWAxis As WorkAxis)
	Dim oInclude As DialogResult = Nothing
	If oWAxis.Name = "Asse Z" Or oWAxis.Name = "Asse X" Or oWAxis.Name = "Asse Y" Then
		oInclude = MessageBox.Show("Includere " & oWAxis.Name & "?", "Includere Asse", _
		MessageBoxButtons.YesNo, MessageBoxIcon.Question)
		If oInclude = DialogResult.Yes Then
			oView.SetIncludeStatus(oWAxis, True)
			'oAxis.Visible = True
			'oAxis.AutoResize = True
		Else
			oView.SetIncludeStatus(oWAxis, False)
		End If
	End If
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

WCrihfield
Mentor
Mentor

I think I may have missed an important step (getting proxy) in that last code, so here is another version that may work better.  This assumes though, that the chosen component is a 'top level' component.  If it is not, more code may be needed.

Sub Main
	Dim View As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Scegli una vista")
	If IsNothing(View) Then Exit Sub
	Dim ViewDoc As Document = View.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oMainAxes As WorkAxes = ViewDoc.ComponentDefinition.WorkAxes
	For Each Axis As WorkAxis In oMainAxes
		ChooseIncludeInView(View, Axis)
	Next
	Dim oDocNames As New List(Of String)
	For Each oRefDoc As Document In ViewDoc.AllReferencedDocuments
		oDocNames.Add(oRefDoc.FullDocumentName)
	Next
	Dim oDocName As String = InputListBox("Choose Sub Doc To Include Axis From", oDocNames, "")
	If oDocName = "" Then Exit Sub
	Dim oChosenOcc As ComponentOccurrence = Nothing
	Dim oOccs As ComponentOccurrences = ViewDoc.ComponentDefinition.Occurrences
	For Each oOcc As ComponentOccurrence In oOccs
		If oOcc.ReferencedDocumentDescriptor.FullDocumentName = oDocName Then
			oChosenOcc = oOcc
			Exit For
		End If
	Next
	If IsNothing(oChosenOcc) Then Exit Sub
	Dim oSubAxes As WorkAxes = oChosenOcc.Definition.WorkAxes
	For Each oSubAxis As WorkAxis In oSubAxes
		Dim oWAProxy As WorkAxisProxy = Nothing
		oChosenOcc.CreateGeometryProxy(oSubAxis, oWAProxy)
		ChooseIncludeInView(View, oWAProxy)
	Next
	'View.Parent.Update 'update the sheet
End Sub

Sub ChooseIncludeInView(oView As DrawingView, oWAxis As WorkAxis)
	Dim oInclude As DialogResult = Nothing
	If oWAxis.Name = "Asse Z" Or oWAxis.Name = "Asse X" Or oWAxis.Name = "Asse Y" Then
		oInclude = MessageBox.Show("Includere " & oWAxis.Name & "?", "Includere Asse", _
		MessageBoxButtons.YesNo, MessageBoxIcon.Question)
		If oInclude = DialogResult.Yes Then
			oView.SetIncludeStatus(oWAxis, True)
			'oAxis.Visible = True
			'oAxis.AutoResize = True
		Else
			oView.SetIncludeStatus(oWAxis, False)
		End If
	End If
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

TecnicoPanni
Participant
Participant

I ecounter an error.

 

-Choose the view (fine)

-Choose the axis for the assembly I want to import by yes/no (fine)

-Choose the part I want to import the axis (fine)

-Choose the axis I want to import by yes/no (if no, no problem - if yes, the following error arises)

 

System.ArgumentException: Incorrect parameter. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.DrawingView.SetIncludeStatus(Object Object, Boolean Include)
in ThisRule.ChooseIncludeInView(DrawingView or View, WorkAxis or WAxis)
in ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes

TecnicoPanni
Participant
Participant

same error position, but different message

 

System.NullReferenceException: Riferimento a un oggetto non impostato su un'istanza di oggetto.
in ThisRule.Main()
in Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
in iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes

WCrihfield
Mentor
Mentor

Hi @TecnicoPanni.  I just tested the last code I posted on one of my own drawings of an assembly, to see if I would encounter any errors myself.  I had to comment out the main If...Then statement within the 'ChooseIncludeInView' sub routine, because my WorkAxis objects were named differently, but left the MessageBox question in there.  Everything worked as planned, and I did not encounter any errors.  I wander if there may be multiple instances of components in your assembly that have the same FullDocumentName?  If so, I may be getting the wrong instance/component to attempt to retrieve the WorkAxis objects from for including in the view.  If this is the case, we may have to loop through components, and show component names in the list, instead of FullDocumentName.  If this is the case, would choosing a component name work OK for you?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

TecnicoPanni
Participant
Participant

it's odd, the sample assembly is composed from only 2 different components.

The error persists.

Please find enclosed this sample assembly 09SMJ8170000.iam.

0 Likes

TecnicoPanni
Participant
Participant

Hello @WCrihfield

I notice that the error rises on string beetwen the MessageBoxes "isolating string1" and "isolating string2"

Sub Main
	Dim View As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Scegli una vista")
	If IsNothing(View) Then Exit Sub
	Dim ViewDoc As Document = View.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oMainAxes As WorkAxes = ViewDoc.ComponentDefinition.WorkAxes
	For Each Axis As WorkAxis In oMainAxes
		ChooseIncludeInView(View, Axis)
	Next
	Dim oDocNames As New List(Of String)
	For Each oRefDoc As Document In ViewDoc.AllReferencedDocuments
		oDocNames.Add(oRefDoc.FullDocumentName)
	Next
	Dim oDocName As String = InputListBox("Choose Sub Doc To Include Axis From", oDocNames, "")
	If oDocName = "" Then Exit Sub
	Dim oChosenOcc As ComponentOccurrence = Nothing
	Dim oOccs As ComponentOccurrences = ViewDoc.ComponentDefinition.Occurrences
	For Each oOcc As ComponentOccurrence In oOccs
					MessageBox.Show("Message", "Isolating string1")
		If oOcc.ReferencedDocumentDescriptor.FullDocumentName = oDocName Then
					MessageBox.Show("Message", "Isolating string2")
			oChosenOcc = oOcc
			Exit For
		End If
	Next
	If IsNothing(oChosenOcc) Then Exit Sub
	Dim oSubAxes As WorkAxes = oChosenOcc.Definition.WorkAxes
	For Each oSubAxis As WorkAxis In oSubAxes
		Dim oWAProxy As WorkAxisProxy = Nothing
		oChosenOcc.CreateGeometryProxy(oSubAxis, oWAProxy)
		ChooseIncludeInView(View, oWAProxy)
	Next
	'View.Parent.Update 'update the sheet
End Sub

Sub ChooseIncludeInView(oView As DrawingView, oWAxis As WorkAxis)
	Dim oInclude As DialogResult = Nothing
	If oWAxis.Name = "Asse Z" Or oWAxis.Name = "Asse X" Or oWAxis.Name = "Asse Y" Then
		oInclude = MessageBox.Show("Includere " & oWAxis.Name & "?", "Includere Asse", _
		MessageBoxButtons.YesNo, MessageBoxIcon.Question)
		If oInclude = DialogResult.Yes Then
			oView.SetIncludeStatus(oWAxis, True)
			'oAxis.Visible = True
			'oAxis.AutoResize = True
		Else
			oView.SetIncludeStatus(oWAxis, False)
		End If
	End If
End Sub

Here belo the error

 

System.NullReferenceException: Object reference not set to an object instance.
in ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

 

Many thanks

 

0 Likes

WCrihfield
Mentor
Mentor

Hi @TecnicoPanni.  Do you have any 'virtual' components in your assembly?  They can be created for BOM purposes, and may show up in your model browser tree, but there is nothing to show in the model screen, and just add a row in your BOM for some items that you may not have a model for, but need them to be listed.  These types of assembly components will not have a 'Document' associated with them, which might cause that error at that point.  Also if you have any weldment type assemblies anywhere within your main assembly, there is often a component within them that just represents the 'welds', and I do not think those will have a 'Document' associated with them.  There are extra lines of code we could be using to avoid working with them, if they are an issue.  Also, if a component is suppressed, you can not do almost anything with them, but you should still be able to check their 'ReferencedDocumentDescriptor.FullDocumentName.  There is a line of code  we could add to avoid working with those suppressed components too, if needed.  What version of Inventor are you using?  If using 2022 or newer, then are there any custom ModelStates involved?

Edit:  Below are some of those filtering lines, if needed, that could be used when looping through assembly components, to help avoid errors.

If oOcc.Suppressed Then Continue For 'skip to next oOcc
If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For
If TypeOf oOcc.Definition Is WeldsComponentDefinition Then Continue For

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

TecnicoPanni
Participant
Participant

Just 3 words: YOU ARE AMAZING!!!

It works! My staff will thank you for every spared second by using this macro.

 

0 Likes

WCrihfield
Mentor
Mentor

@TecnicoPanni Here is a slightly different version of the code.  This version does not use the 'AllReferencedDocuments' reference, and instead just loops through the assembly components, while filtering out any that are suppressed, virtual, or welds, then collects their names directly, as seen in the model browser tree.  This version will only with 'top level' components though, not any sub components.  It could be modified a bit to include sub components though, if needed.  Then it lets you pick one of the component names.  Gets the component with that chosen name (will be unique).  No need for messing around with checking 'FullDocumentName', because it is no longer needed.  Then it proceeds to use that component for WorkAxis inclusion in the view.

Sub Main
	Dim View As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Scegli una vista")
	If IsNothing(View) Then Exit Sub
	Dim ViewDoc As Document = View.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oMainAxes As WorkAxes = ViewDoc.ComponentDefinition.WorkAxes
	For Each Axis As WorkAxis In oMainAxes
		ChooseIncludeInView(View, Axis)
	Next
	Dim oOccNames As New List(Of String)
	Dim oOccs As ComponentOccurrences = ViewDoc.ComponentDefinition.Occurrences
	For Each oOcc As ComponentOccurrence In oOccs
		If oOcc.Suppressed Then Continue For 'skip to next oOcc
		If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For
		If TypeOf oOcc.Definition Is WeldsComponentDefinition Then Continue For
		oOccNames.Add(oOcc.Name)
	Next
	Dim oOccName As String = InputListBox("Choose Component To Include Axis From", oOccNames, "")
	If oOccName = "" Then Exit Sub
	Dim oChosenOcc As ComponentOccurrence = oOccs.ItemByName(oOccName)
	Dim oSubAxes As WorkAxes = oChosenOcc.Definition.WorkAxes
	For Each oSubAxis As WorkAxis In oSubAxes
		Dim oWAProxy As WorkAxisProxy = Nothing
		oChosenOcc.CreateGeometryProxy(oSubAxis, oWAProxy)
		ChooseIncludeInView(View, oWAProxy)
	Next
	'View.Parent.Update 'update the sheet
End Sub

Sub ChooseIncludeInView(ByRef oView As DrawingView, oWAxis As WorkAxis)
	Dim oInclude As DialogResult = Nothing
	If oWAxis.Name = "Asse Z" Or oWAxis.Name = "Asse X" Or oWAxis.Name = "Asse Y" Then
		oInclude = MessageBox.Show("Includere " & oWAxis.Name & "?", "Includere Asse", _
		MessageBoxButtons.YesNo, MessageBoxIcon.Question)
		If oInclude = DialogResult.Yes Then
			oView.SetIncludeStatus(oWAxis, True)
			'oWAxis.Visible = True
			'oWAxis.AutoResize = True
		Else
			oView.SetIncludeStatus(oWAxis, False)
		End If
	End If
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

TecnicoPanni
Participant
Participant

@WCrihfield

hope this is the last time I challenge your patience.

Please, le me know how can this latest rule be modified to include sub components.

Thank you!!!

 

0 Likes