Reaching to Named Entities via Visual studio

Reaching to Named Entities via Visual studio

Mehmet_Fatih_Turker
Advocate Advocate
262 Views
2 Replies
Message 1 of 3

Reaching to Named Entities via Visual studio

Mehmet_Fatih_Turker
Advocate
Advocate

Hi all,

I had problem with reaching to iLogicVb via VB.NET

 

Function ApplyChamfer(chamferDistance As String)
	
    Try
		
	Dim oPDoc As PartDocument = g_inventorApplication.ActiveDocument
        Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
        Dim oNamedEntities As NamedEntities = iLogicVb.Automation.GetNamedEntities(oPDoc)

        Dim yzPlaneCrossing As Boolean
        Dim xzPlaneCrossing As Boolean

        Dim faceCounter As Integer = 1 ' Counters for naming faces and edges
        Dim edgeCounter As Integer = 1 ' Counters for naming faces and edges

        For Each oFace As Face In oPDef.SurfaceBodies.Item(1).Faces ' Iterate over all faces in the part body
			
            yzPlaneCrossing = False
            xzPlaneCrossing = False

            For Each oEdge As Edge In oFace.Edges ' Check if face crosses the YZ plane and/or XZ plane
				
                Dim startVertex As Vertex = oEdge.StartVertex
                Dim stopVertex As Vertex = oEdge.StopVertex

                Dim startX As Double = startVertex.Point.X
                Dim stopX As Double = stopVertex.Point.X
                Dim startY As Double = startVertex.Point.Y
                Dim stopY As Double = stopVertex.Point.Y

                
                If (startX < 0 And stopX > 0) Or (startX > 0 And stopX < 0) Or (Math.Abs(startX) < 0.01) Or (Math.Abs(stopX) < 0.01) Then ' Check if the edge crosses the YZ plane (x-coordinate crosses 0)
					
                    yzPlaneCrossing = True
					
                End If

                
                If (startY < 0 And stopY > 0) Or (startY > 0 And stopY < 0) Or (Math.Abs(startY) < 0.01) Or (Math.Abs(stopY) < 0.01) Then
					
                    xzPlaneCrossing = True
					
                End If
				
            Next

            If yzPlaneCrossing And Not xzPlaneCrossing Then 
                
                Dim faceName As String = "faceForChamfer_" & faceCounter
                
				Try
                    
					oNamedEntities.SetName(oFace, faceName)
					
                    Logger.Info("Assigned name to face: " & faceName)
                
				Catch ex As Exception
					
                    MsgBox("Failed to assign name to face: " & ex.Message)
					
                End Try

                For Each oEdge As Edge In oFace.Edges  
					
                    Dim startVertex As Vertex = oEdge.StartVertex
                    Dim stopVertex As Vertex = oEdge.StopVertex
                    
                    Dim startZ As Double = startVertex.Point.Z
                    Dim stopZ As Double = stopVertex.Point.Z
                    
                    If startZ = stopZ Then 
						
                        Dim edgeName As String = "edgeForChamfer_" & edgeCounter
						
                        Try
							
                            oNamedEntities.SetName(oEdge, edgeName)
							
                            Logger.Info("Assigned name to edge: " & edgeName)
							
                        Catch ex As Exception
							
                            MsgBox("Failed to assign name to edge: " & ex.Message)
							
                        End Try
						
                        edgeCounter += 1
						
                    End If
					
                Next

                faceCounter += 1 
            
			End If
        
		Next

        Dim edge1 As Edge = oNamedEntities.FindEntity("edgeForChamfer_1") 
        Dim edge2 As Edge = oNamedEntities.FindEntity("edgeForChamfer_2")
        Dim edge3 As Edge = oNamedEntities.FindEntity("edgeForChamfer_3")
        Dim edge4 As Edge = oNamedEntities.FindEntity("edgeForChamfer_4")
        Dim faceForChamfer2 As Face = oNamedEntities.FindEntity("faceForChamfer_2")
		Dim faceForChamfer1 As Face = oNamedEntities.FindEntity("faceForChamfer_1")
        
		Dim edgetoChamfer1 As EdgeCollection = ThisApplication.TransientObjects.CreateEdgeCollection() 
        Dim edgetoChamfer2 As EdgeCollection = ThisApplication.TransientObjects.CreateEdgeCollection() 

        edgetoChamfer1.Add(edge1)
        edgetoChamfer1.Add(edge2) '	edgetoChamfer1.Add(oNamedEntities.Entities.Value("edgeForChamfer_2"))
        edgetoChamfer2.Add(edge3)
        edgetoChamfer2.Add(edge4)

        Dim chamferEdge As ChamferFeature = oPDef.Features.ChamferFeatures.AddUsingDistance(edgetoChamfer1, chamferDistance, False, False, False) 
        Dim chamferEdge2 As ChamferFeature = oPDef.Features.ChamferFeatures.AddUsingDistanceAndAngle(edgetoChamfer2, faceForChamfer2, chamferDistance, "45", False, False)
    Catch ex As Exception
		
        MsgBox("Error during chamfer operation: " & ex.Message)
    
	End Try
	
End Function

 

So I researched a bit then found out that I was able to reach in this way :

 

 

Dim oPDoc As PartDocument = CType(g_inventorApplication.ActiveDocument, PartDocument) 
PartComponentDefinition = oPDoc.ComponentDefinition ' Obtain IiLogicAutomation instance 
Dim iLogicAuto As IiLogicAutomation =CType(g_inventorApplication.Automation, IiLogicAutomation)
Dim oNamedEntities As NamedEntities = iLogicAuto.GetNamedEntities(oPDoc) 

 

But it gives casting error. 

 

I can use AttributeSets in order to reach those entities but is there any way to react to named entities by using IiLogicAutomation or iLogicVb somehow ?

0 Likes
Accepted solutions (2)
263 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Mehmet_Fatih_Turker.  That separate little block of code after your main code window does not look correct to me.  You do not get the 'Automation' object directly from the Inventor.Application object, which I believe that variable represents, but from the ApplicationAddIn object represenging the iLogic add-in.  So, you would have to find/get the ApplicationAddIn object for iLogic first, then use its Automation property.  That property does not have a meaningful value for most add-ins, but it does for the iLogic add-in, because it has its own API exposed for us.  It just returns a generic Object type, but you should be able to 'cast' it to that IiLogicAutomation Interface type.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

Mehmet_Fatih_Turker
Advocate
Advocate
Accepted solution

Hi @WCrihfield  thank for your kind response. I was getting this error at some point;

 

Error during chamfer operation: Unable to cast COM object of type 'System.__ComObject' to interface type 'Autodesk.iLogic.Interfaces.IiLogicAutomation'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{91DF0977-6AF7-305B-A571-67AF76D7A4C9}' failed due to the following error: Böyle bir arabirim desteklenmiyor (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).


I  searched for solution then found this . 

But as I attached above, the error I was getting is guiding me to declare ID as {91DF0977-6AF7-305B-A571-67AF76D7A4C9}. I tried that but I kept getting error again. Then after your response, I tried with usual ID that everyone in forum was using "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}" and I menaged to reach to entities. 

 

            Dim oPDoc As PartDocument = g_inventorApplication.ActiveDocument
            Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
            Const iLogicAddinGuid As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"
            Dim addin As ApplicationAddIn = g_inventorApplication.ApplicationAddIns.ItemById(iLogicAddinGuid)
            Dim iLogicAuto As Object = addin.Automation
            Dim oNamedEntities As NamedEntities = iLogicAuto.GetNamedEntities(oPDoc)

so its just that simple but after inputting iLogicAuto, visual studio doesnt offer me any method or class.

is it possible to fetch those methods ?

0 Likes