I don't know if this is possible or not. But I'm trying to identify named entities. I'm able to grab the entities I need from the constraints at the assembly level "oEnt1" & "oEnt2". Now I'm trying to look at those entities and determine what their names are. "Face0" for example. Then if they don't have names, assign them names.
Dim oEnt1 As Object = oConst.EntityOne Dim oEnt2 As Object = oConst.EntityTwo
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
Solved by WCrihfield. Go to Solution.
Hi @ndillner343SKL. Yes, that is possible, but it can be fairly complicated behind the scenes. I actually created a custom function some time back just for retrieving assigned names from various possible objects that may have been assigned a name using the built-in NamedEntities interface. The entities you will get from the EntityOne or EntityTwo of an assembly constraint will almost always be a proxy of some sort, that only exists within the context of the assembly, so they won't have the name assigned directly to them. The name will be assigned to the 'native' object that the proxy was a copy of (the actual geometry within the context of a part). Of course it is possible that one side of your constraint was to one of the assemblies own work features, in which case it will not be a proxy though. Either way, you can feed the object you get from that EntityOne to this function, and the function will either return its name, or an empty String. You would just have to check the returned value to make sure it is not empty, before trying to use it for anything important.
Keep in mind, to use this function, your other iLogic rule code would need to be within the Sub Main...End Sub boundaries, then this whole function below, would be placed below the End Sub somewhere. Then somewhere within your Sub Main area, you just need to declare a String type variable and set its value by calling this function to run, and put that object as its input variable.
Here is that function's code:
Function GetEntityName(oEntity As Object) As String
Dim oType As String = TypeName(oEntity)
Dim oDoc As Document = Nothing
Dim oFace As Face = Nothing
Dim oEdge As Edge = Nothing
Dim oVertex As Vertex = Nothing
Dim oNativeObject As Object = Nothing
Select Case oType
Case "Face"
oFace = oEntity
oDoc = oFace.Parent.ComponentDefinition.Document
Case "FaceProxy"
Dim oFaceProxy As FaceProxy = oEntity
If TypeOf oFaceProxy.NativeObject Is FaceProxy Then
GetEntityName(oFaceProxy.NativeObject)
Else
oFace = oFaceProxy.NativeObject
End If
oNativeObject = oFace
oDoc = oFace.Parent.ComponentDefinition.Document
Case "Edge"
oEdge = oEntity
oDoc = oEdge.Parent.ComponentDefinition.Document
Case "EdgeProxy"
Dim oEdgeProxy As EdgeProxy = oEntity
If TypeOf oEdgeProxy.NativeObject Is EdgeProxy Then
GetEntityName(oEdgeProxy.NativeObject)
Else
oEdge = oEdgeProxy.NativeObject
End If
oNativeObject = oEdge
oDoc = oEdge.Parent.ComponentDefinition.Document
Case "Vertex"
oVertex = oEntity
oDoc = oVertex.Parent.ComponentDefinition.Document
Case "VertexProxy"
Dim oVertexProxy As VertexProxy = oEntity
If TypeOf oVertexProxy.NativeObject Is VertexProxy Then
GetEntityName(oVertexProxy.NativeObject)
Else
oVertex = oVertexProxy.NativeObject
End If
oNativeObject = oVertex
oDoc = oVertex.Parent.ComponentDefinition.Document
End Select
If IsNothing(oDoc) Then Return ""
Dim oNEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oDoc)
Dim oName As String
If IsNothing(oNativeObject) Then
oName = oNEs.GetName(oEntity)
Else
oName = oNEs.GetName(oNativeObject)
End If
Return oName
End Function
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
(Not an Autodesk Employee)
Here is a similar custom Sub routine I just created, for attempting to assign a name to an object that might possibly be a proxy in a scenario like that. This might fail if the input object is a work feature, but work features can be named easily enough through other means. You might just have to test for what type of object it is ahead of time. I have not tried using this one yet, because I have not needed to use it before, so try it at your own risk.
Sub AssignNameToEntity(oEntity As Object, oName As String)
Dim oType As String = TypeName(oEntity)
Dim oPDoc As PartDocument = Nothing
Dim oFace As Face = Nothing
Dim oEdge As Edge = Nothing
Dim oVertex As Vertex = Nothing
Dim oNativeObject As Object = Nothing
Select Case oType
Case "Face"
oFace = oEntity
oPDoc = oFace.Parent.ComponentDefinition.Document
Case "FaceProxy"
Dim oFaceProxy As FaceProxy = oEntity
If TypeOf oFaceProxy.NativeObject Is FaceProxy Then
AssignNameToEntity(oFaceProxy.NativeObject, oName)
Else
oFace = oFaceProxy.NativeObject
End If
oNativeObject = oFace
oPDoc = oFace.Parent.ComponentDefinition.Document
Case "Edge"
oEdge = oEntity
oPDoc = oEdge.Parent.ComponentDefinition.Document
Case "EdgeProxy"
Dim oEdgeProxy As EdgeProxy = oEntity
If TypeOf oEdgeProxy.NativeObject Is EdgeProxy Then
AssignNameToEntity(oEdgeProxy.NativeObject, oName)
Else
oEdge = oEdgeProxy.NativeObject
End If
oNativeObject = oEdge
oPDoc = oEdge.Parent.ComponentDefinition.Document
Case "Vertex"
oVertex = oEntity
oPDoc = oVertex.Parent.ComponentDefinition.Document
Case "VertexProxy"
Dim oVertexProxy As VertexProxy = oEntity
If TypeOf oVertexProxy.NativeObject Is VertexProxy Then
AssignNameToEntity(oVertexProxy.NativeObject, oName)
Else
oVertex = oVertexProxy.NativeObject
End If
oNativeObject = oVertex
oPDoc = oVertex.Parent.ComponentDefinition.Document
End Select
If IsNothing(oPDoc) Then Exit Sub
Dim oNEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oPDoc)
If IsNothing(oNativeObject) Then
Try
oNEs.SetName(oEntity, oName)
Catch oEx As Exception
MsgBox("Error trying to set name to supplied entity." & vbCrLf & _
oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "")
'Logger.Error("Error trying to set name to supplied entity." & vbCrLf & _
'oEx.Message & vbCrLf & oEx.StackTrace)
End Try
Else
Try
oNEs.SetName(oNativeObject, oName)
Catch oEx As Exception
MsgBox("Error trying to set name to 'native object' of supplied entity." & vbCrLf & _
oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "")
'Logger.Error("Error trying to set name to 'native object' of supplied entity." & vbCrLf & _
'oEx.Message & vbCrLf & oEx.StackTrace)
End Try
End If
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
(Not an Autodesk Employee)
Hi @WCrihfield,
Is there a way to preform this same process but with the source face. I'm working within an assembly and instead of grabbing the assembly level ProxyFace. I'd like to grab the face that's being used to create the proxy face and name it.
I tried having it grab the source face when setting oEnt1 & 2 as Objects. But it doesn't return any value.
Dim oEnt1 As Object = oConst.EntityOne.NativeObject.GetSourceFace
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 x As Integer = 0 Dim oADoc As AssemblyDocument = ThisDoc.Document Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition Dim oConsts As AssemblyConstraints = oADef.Constraints For Each oConst As AssemblyConstraint In oConsts Dim oEnt1 As Object = oConst.EntityOne.NativeObject.GetSourceFace Dim oEnt2 As Object = oConst.EntityTwo.NativeObject.GetSourceFace x = x + 1 oName = "Entity1:" & x Call AssignNameToEntity(oEnt1, oName) Dim EntityOne As String = GetEntityName(oEnt1) MsgBox(EntityOne) oName = "Entity2:" & x Call AssignNameToEntity(oEnt2, oName) Dim EntityTwo As String = GetEntityName(oEnt2) MsgBox(EntityTwo) Next End Sub Function GetEntityName(oEntity As Object) As String Dim oType As String = TypeName(oEntity) Dim oDoc As Document = Nothing Dim oFace As Face = Nothing Dim oEdge As Edge = Nothing Dim oVertex As Vertex = Nothing Dim oNativeObject As Object = Nothing Select Case oType Case "Face" oFace = oEntity oDoc = oFace.Parent.ComponentDefinition.Document Case "FaceProxy" Dim oFaceProxy As FaceProxy = oEntity If TypeOf oFaceProxy.NativeObject Is FaceProxy Then GetEntityName(oFaceProxy.NativeObject) Else oFace = oFaceProxy.NativeObject End If oNativeObject = oFace oDoc = oFace.Parent.ComponentDefinition.Document Case "Edge" oEdge = oEntity oDoc = oEdge.Parent.ComponentDefinition.Document Case "EdgeProxy" Dim oEdgeProxy As EdgeProxy = oEntity If TypeOf oEdgeProxy.NativeObject Is EdgeProxy Then GetEntityName(oEdgeProxy.NativeObject) Else oEdge = oEdgeProxy.NativeObject End If oNativeObject = oEdge oDoc = oEdge.Parent.ComponentDefinition.Document Case "Vertex" oVertex = oEntity oDoc = oVertex.Parent.ComponentDefinition.Document Case "VertexProxy" Dim oVertexProxy As VertexProxy = oEntity If TypeOf oVertexProxy.NativeObject Is VertexProxy Then GetEntityName(oVertexProxy.NativeObject) Else oVertex = oVertexProxy.NativeObject End If oNativeObject = oVertex oDoc = oVertex.Parent.ComponentDefinition.Document End Select If IsNothing(oDoc) Then Return "" Dim oNEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oDoc) Dim oName As String If IsNothing(oNativeObject) Then oName = oNEs.GetName(oEntity) Else oName = oNEs.GetName(oNativeObject) End If Return oName End Function Sub AssignNameToEntity(oEntity As Object, oName As String) Dim oType As String = TypeName(oEntity) Dim oPDoc As PartDocument = Nothing Dim oFace As Face = Nothing Dim oEdge As Edge = Nothing Dim oVertex As Vertex = Nothing Dim oNativeObject As Object = Nothing Select Case oType Case "Face" oFace = oEntity oPDoc = oFace.Parent.ComponentDefinition.Document Case "FaceProxy" Dim oFaceProxy As FaceProxy = oEntity If TypeOf oFaceProxy.NativeObject Is FaceProxy Then AssignNameToEntity(oFaceProxy.NativeObject, oName) Else oFace = oFaceProxy.NativeObject End If oNativeObject = oFace oPDoc = oFace.Parent.ComponentDefinition.Document Case "Edge" oEdge = oEntity oPDoc = oEdge.Parent.ComponentDefinition.Document Case "EdgeProxy" Dim oEdgeProxy As EdgeProxy = oEntity If TypeOf oEdgeProxy.NativeObject Is EdgeProxy Then AssignNameToEntity(oEdgeProxy.NativeObject, oName) Else oEdge = oEdgeProxy.NativeObject End If oNativeObject = oEdge oPDoc = oEdge.Parent.ComponentDefinition.Document Case "Vertex" oVertex = oEntity oPDoc = oVertex.Parent.ComponentDefinition.Document Case "VertexProxy" Dim oVertexProxy As VertexProxy = oEntity If TypeOf oVertexProxy.NativeObject Is VertexProxy Then AssignNameToEntity(oVertexProxy.NativeObject, oName) Else oVertex = oVertexProxy.NativeObject End If oNativeObject = oVertex oPDoc = oVertex.Parent.ComponentDefinition.Document End Select If IsNothing(oPDoc) Then Exit Sub Dim oNEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oPDoc) If IsNothing(oNativeObject) Then Try oNEs.SetName(oEntity, oName) Catch oEx As Exception MsgBox("Error trying to set name to supplied entity." & vbCrLf & _ oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "") 'Logger.Error("Error trying to set name to supplied entity." & vbCrLf & _ 'oEx.Message & vbCrLf & oEx.StackTrace) End Try Else Try oNEs.SetName(oNativeObject, oName) Catch oEx As Exception MsgBox("Error trying to set name to 'native object' of supplied entity." & vbCrLf & _ oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "") 'Logger.Error("Error trying to set name to 'native object' of supplied entity." & vbCrLf & _ 'oEx.Message & vbCrLf & oEx.StackTrace) End Try End If End Sub
Hi @ndillner343SKL. Your two lines of code that are attempting to retrieve the 'native' source face may not be returning the face you want, if anything at all. This can be a tricky situation, because if the constraint is to a face that belongs to a component that is down within one or more sub-assemblies, the 'NativeObject' may still be a 'proxy' type object (the proxy of that face from within the sub-assembly's context). And I'm not 100% sure if the 'GetSourceFace' method is a good replacement for the NativeObject property for retrieving the true face of the part, because I simply haven't used it much before.
Dim oEnt1 As Object = oConst.EntityOne.NativeObject.GetSourceFace
Dim oEnt2 As Object = oConst.EntityTwo.NativeObject.GetSourceFace
I designed those two custom routines so that they will 'recursively' dig down to the true source geometry object in the context of a part before they will try to retrieve or set a name, so you should not really need to dig down to the source object before supplying it to either of those two routines. However, they should still work just fine if you did supply the native/source objects directly to them.
Wesley Crihfield
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.