iLogic Named.Entities only in component and not assembly available

iLogic Named.Entities only in component and not assembly available

arnoldQ3YQQ
Contributor Contributor
2,222 Views
7 Replies
Message 1 of 8

iLogic Named.Entities only in component and not assembly available

arnoldQ3YQQ
Contributor
Contributor

Hi

i'm trying to find components in an assembly through named faces. But weirdly it finds them in component view and not in the assembly, where just this one component with a face named "GSTop" is inserted.

 

 

Dim namedEntities = ThisDoc.NamedEntities
found = namedEntities.NameExists("GSTop")
Logger.Info(found)

 

 

true.jpg

(First "false" is executed rule in assembly, second "true" is the same rule executed in component view)

Thanks

0 Likes
Accepted solutions (2)
2,223 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Hi @arnoldQ3YQQ.  I believe that named entities only exist in Part type documents right now, so you won't find them directly within any assembly.  Also, nothing 'real' actually exists within assemblies.  Just work features an proxy data.  All the 'real' geometry is within the Part files themselves.  If you want to retrieve a face within an assembly that you named within a part, you will have to go through a round about process of defining a FaceProxy (a copy of the Face that exists only in the assembly model space) of the actual Face, then use that FaceProxy instead of the real one for things like constraints, measurements, and similar functionality within the assembly.

 

There are a few ways to get the named entities from a part too.

One way is using 'ThisDoc.NamedEntities' like you were using.  But beware using that line, because you may not be aware of what actual document it is trying to retrieve those named entities from.  The term ThisDoc will default to pointing to the 'local' document (the same document that the rule is saved within if it is a local rule) first, and if there is no 'local' document (like when you are using an external rule), it will then target whichever document was 'active' when the rule first started.

example:

oNEs = ThisDoc.NamedEntities

Another way is like this:   (which allows you to specify a Document for it to target)

oNEs = iLogicVb.Automation.GetNamedEntities(oDoc)

 (Where the variable 'oDoc' has had its value set to a Document or PartDocument)

The other way is to search for them using the AttributeManager, and the methods defined under it to find all the AttributeSets within a PartDocument with that certain, specific name ("iLogicEntityNameSet"), then if those Sets contain an Attribute with a certain name ("iLogicEntityName"), then get the object that the AttributeSet was attached to.  That's the older, and more labor intense way of doing it, before those iLogic snippets existed.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

arnoldQ3YQQ
Contributor
Contributor

Thanks for the answer.
To clarify, getting any named entities (faces) via iLogic is not possible in assembly?

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor

Sort of...  Those named faces don't exist directly within that assembly, but you could technically find those named faces within the documents being represented by the components, then create the FaceProxy of those named faces, then use those FaceProxy objects for whatever purpose you might have had in mind.  A FaceProxy object is exactly the same as a regular Face object.  The main difference is that the regular Face object is in the context of the part's model space, while the FaceProxy object is in the context of the assembly's model space.  If you want to create a constraint between faces or between a face and something else in an assembly by code ; or you want to measure between faces or between a face and something else in an assembly, you need to use those FaceProxy objects to do so.  The FaceProxy object (and other proxy type objects) are created using the ComponentOccurrence.CreateGeometryProxy method.  The ComponentOccurrence you use to do that must be the same ComponentOccurrence that the actual geometry exists within, that you want to make a proxy of.

 

So basically, to find and get a FaceProxy of a named face, you need to get the component object, then get the document object that that component represents, then use something like the GetNamedEntities method to find the named face in that document and set it as the value of a variable, then supply that variable to the CreateGeometryProxy method of the component, along with a new variable (as the second input variable) to hold the FaceProxy object that it will create for you.  Yes...it is a bit complicated.

 

What are you wanting to do with it if/when you do find the named face?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Here is the iLogic code I developed for the task of finding every component with that named face, creating the needed FaceProxy objects from those named faces, then creating a series of Flush constraints to make all those parts in the assembly flush with each other at that face.  This demonstrates how to find the named entities in 'top level' assembly components, and how to use the FaceProxy objects for creating assembly level constraints.  I hope this helps some folks out along their Inventor automation journeys.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	oADef = oADoc.ComponentDefinition
	oOccs = oADef.Occurrences
	Dim oFaceProxys As New List(Of FaceProxy)
	For Each oOcc As ComponentOccurrence In oOccs
		'if it does not represent a Part, then skip to next oOcc
		If oOcc.ReferencedDocumentDescriptor.ReferencedDocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		Dim oOccDoc As PartDocument = oOcc.ReferencedDocumentDescriptor.ReferencedDocument
		oNEs = iLogicVb.Automation.GetNamedEntities(oOccDoc)
		'if this name is not found within, then skip to next oOcc
		If Not oNEs.NameExists("GSTop") Then Continue For
		Dim oFace As Face
		'make sure it found something, and that it is a Face,
		'before assigning it as the value of a Face Type variable, to avoid potential error
		oObj = oNEs.TryGetEntity("GSTop")
		If oObj IsNot Nothing AndAlso TypeOf oObj Is Face Then
			oFace = oObj
		End If
		If oFace Is Nothing Then Continue For
		Dim oFaceProxy As FaceProxy
		'set the value of our oFaceProxy variable by creating it
		'this proxy object is created in the context of the assembly in which
		'this component is 
		oOcc.CreateGeometryProxy(oFace, oFaceProxy)
		oFaceProxys.Add(oFaceProxy)
	Next
	oConsts = oADef.Constraints
	'the FaceProxy's must be a 'planar face' (flat) or this won't work
	Dim oFirstFP As FaceProxy = oFaceProxys.First
	For Each oFP In oFaceProxys
		If oFP IsNot oFirstFP Then
			oFlush = oConsts.AddFlushConstraint(oFirstFP, oFP, 0)
		End If
	Next
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) 👍.

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8

matteo_pallaroni
Advocate
Advocate

Hi all, sorry for being dumb but how this could work without defining oNEs as something?

oNEs = iLogicVb.Automation.GetNamedEntities(oOccDoc)

I always get a casting error...

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

Hi @matteo_pallaroni.  Are you using this in a regular iLogic rule, or somewhere else, like within a Visual Studio vb.net code, or an Inventor ApplicationAddIn?  If used within an iLogic rule you should not have any trouble, but it depents on your settings.  If you use something like 'Option Strict On'.  Below are two more ways you can do that line of code:

Dim oNEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oOccDoc)

...or

Dim oNEs As Autodesk.iLogic.Interfaces.NamedEntities = iLogicVb.Automation.GetNamedEntities(oOccDoc)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

matteo_pallaroni
Advocate
Advocate

Looks nice. Thankyou for taking care

I'keep you posted

0 Likes