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

ILOGIC: Open selected parts from assembly and run some code inside it

amarcoc
Advocate

ILOGIC: Open selected parts from assembly and run some code inside it

amarcoc
Advocate
Advocate

Hi.

 

IS there any ilogic avaiable to open selected parts from assembly and run some code inside it?

 

Any idea?

 

Thanks!

0 Likes
Reply
3,913 Views
6 Replies
Replies (6)

tdant
Collaborator
Collaborator

Almost certainly yes, but that depends on a few definitions. How do you intend to select the components? What do you mean by "run some code inside" the components?

0 Likes

lmc.engineering
Advocate
Advocate

You can do pretty much what ever you can imagine up.. just a case of finding what you need to make it happen.

 

Here's something I've knocked up. Run it as an external rule. 

 

Version:1.0 StartHTML:00000145 EndHTML:00005278 StartFragment:00000294 EndFragment:00005246 StartSelection:00000294 EndSelection:00000294SyntaxEditor Code Snippet

Sub Main()
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oPart As ComponentOccurrence
Dim sRuleName As String = "My rule Name"
Line1 :
'''Pick part occurrence
oPart = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select Part")
	If oPart Is Nothing Then
		Exit Sub
	Else
		Dim oFileName As String = oPart.Definition.Document.FullFileName
		Dim oDoc As PartDocument = ThisApplication.Documents.Open(oFileName)
		auto = iLogicVb.Automation
			Try
			auto.RunRule(oDoc, sRuleName)
			Catch
				MessageBox.Show("Cannot find a rule with the name " & sRuleName & "." & vbLf & "Please try again.", "Open and run")
			End Try
		'''Close the document with SAVE (as False), without SAVE (As True)
		oDoc.Close(False)
	
		Question = MessageBox.Show("Repeat Command?", "Open and run", MessageBoxButtons.OKCancel)
		If Question = vbOK Then
			''Repeat command
			GoTo Line1
		Else
			Exit Sub
		End If
	End If
End Sub  

 

Chris.Brough
Enthusiast
Enthusiast

Hello, i'm trying to run an external rule to act on the opened part and i'm getting the following error.

Error in rule: no_sculpt, in document: FRONT DESK STRUCTURE.iam

Unable to cast COM object of type 'Inventor._DocumentClass' to interface type 'Inventor.PartDocument'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{29F0D463-C114-11D2-B77F-0060B0F159EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

it seems this is an issue with running the rule at the context of the assembly?

 

any thoughts on making this work?

0 Likes

WCrihfield
Mentor
Mentor

Hi @Chris.Brough .

If you have an assembly open and active when the code starts, no matter if you're in-place-editing a part within the main assembly, the assembly is still the active document environment to the code.  And Sculpt is only available at the Part modeling level.

So, to access that feature tool from an open assembly, you would have to first dig down through the object structure to the Part Document.  There's a few ways of doing that.  You could have the program pause to let you manually select the part within the assembly, or you could select it by name from the group of already open documents within Inventor, or you could select it by name through the ComponentOccurrences.

If you go the Occurrences route, you then get the Component's Definition.  Then define it as a PartComponentDefinition.  Then down to it's Features (or PartFeatures).  Then down to SculptFeatures.  Then SculptFeatures.Add(fill in the options).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

ramji.m
Participant
Participant

Hi,

There's a few ways of doing that.  You could have the program pause to let you manually select the part within the assembly, --->  Can you please show this in code pls?

0 Likes

WCrihfield
Mentor
Mentor

Hi @ramji.m.  There is an example of this in the earlier post.  It is a method defined under the CommandManager called 'Pick'.  It is a very popular tool that a lot of folks use that will pause your iLogic rule and wait for you to either manually select something in your model window, or cancel (Esc) out of the command.  Then the iLogic rule will continue with any code below that point in the rule.  It uses the SelectionFilterEnum, to allow you to specify/set which types of objects you want to select, and you can also specify a prompt message that will appear either next to your pointer or in the status bar, while you are in that manual selection mode.  You can use it to return your selected item to a variable within your rule, to you can work with that object through that variable.  It is common practice to use some code following this line in your rule, that makes sure you have selected something (in case you canceled the selection without selecting anything).  It may also be necessary to define that variable ahead of time as the type of object you are expecting, or check what type of object it is afterwards then create that type of variable then set the object as its value.  Once that variable's type is set right, you can access that variable's available properties, methods, events, etc.

Dim oOcc As ComponentOccurrence
oOcc = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "'Select an assembly occurrence.")
If oOcc Is Nothing Then
	MsgBox("You did not select anything. Exiting.", , "")
	Exit Sub
Else
	MsgBox("You selected an assembly occurrrence named '" & oOcc.Name & "'.",,"")
End If

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)