Edge selection and chamfering

Edge selection and chamfering

Mehmet_Fatih_Turker
Advocate Advocate
418 Views
2 Replies
Message 1 of 3

Edge selection and chamfering

Mehmet_Fatih_Turker
Advocate
Advocate

Hi all, 

 

Im trying to pick certain edges on certain faces that crosses with yz plane. I will chamfer this certain edge. While struggling with codes I asked to gpt for help and we shaped this code together.

 

Sub Main()	
    Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
    Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
    Dim Highlighter As HighlightSet = oPDoc.CreateHighlightSet()
    Dim foundFace As Face = Nothing 
    Dim yzPlane As WorkPlane = oPDef.WorkPlanes.Item(2) 
    For Each oFace As Face In oPDef.SurfaceBodies.Item(1).Faces
        Dim faceNormal As UnitVector = oFace.Evaluator.GetNormalAtPoint(oFace.PointOnFace)
        If Math.Abs(faceNormal.X) < 0.001 AndAlso Math.Abs(faceNormal.Y) < 0.001 Then            
            For Each oEdge As Edge In oFace.Edges
                Dim startX As Double = oEdge.StartVertex.Point.X
                Dim stopX As Double = oEdge.StopVertex.Point.X
                If (startX < 0 And stopX > 0) Or (startX > 0 And stopX < 0) Or Math.Abs(startX) < 0.001 Or Math.Abs(stopX) < 0.001 Then
                    foundFace = oFace
                    Exit For
                End If
            Next
            If foundFace IsNot Nothing Then Exit For 
        End If
    Next
    If foundFace IsNot Nothing Then
        Highlighter.AddItem(foundFace)
        MsgBox("Face crossing the YZ plane and parallel to the Z-axis found and highlighted.")
        Dim topBottomEdges As EdgeCollection = ThisApplication.TransientObjects.CreateEdgeCollection()
        For Each oEdge As Edge In foundFace.Edges
            Dim zStart As Double = oEdge.StartVertex.Point.Z
            Dim zStop As Double = oEdge.StopVertex.Point.Z
            If Math.Abs(zStart - zStop) > 0.001 Then
                topBottomEdges.Add(oEdge)
            End If
        Next
        If topBottomEdges.Count > 0 Then
            Dim chamferDistance As Parameter = oPDef.Parameters.Item("Length_1")
            Dim chamferValue As Double = chamferDistance._Value
            oPDef.Features.ChamferFeatures.AddUsingDistance(topBottomEdges, chamferValue, False, False, False)
        Else
            MsgBox("No valid edges found for chamfering.")
        End If
    Else
        MsgBox("No face parallel to the Z-axis and crossing the YZ plane found.")
    End If
End Sub

 

But GPT keeps offering line 

Dim faceNormal As UnitVector = oFace.Evaluator.GetNormalAtPoint(oFace.PointOnFace)

However, ilogic gives me error on this line. Probably types didnt match.  İs there any solution offer available ? Also is there any other way to chamfer always same edge that crosses with yz plane showed below ?

Mehmet_Fatih_Turker_0-1727792493727.png

 

Mehmet_Fatih_Turker_2-1727792657091.png

 

 

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

J_Pfeifer_
Advocate
Advocate
Accepted solution

There are a couple of ways to get to the edges you may need. I have used the face collection, searching through and evaluating the area of the face to verify that, this face is the one I need edges from. From there you can guess or target the edges of that face by targeting the edge collection of that item and comparing the known length. 

Typically if I need the edge, I make sure I have the correct face with a size comparison. Then I draw on a paper what edge is which. 

 

The below code is a modified version of some of the code deep in one of my programs. This should give you a good idea how you can target and select different faces or edges from the evaluator. This could be slightly modified to return all the lengths of each line on a face object. 

 

The edge length is not something I have implemented in my own code, but in the for loop I wrote below the main program logs it seemingly matched the proper distance of the edge it was targetting. If you find small differences, you can implement a tolerance check similar to my first for loop. Note, That first loop is checking the size of the end of a tube face. So OD cir area - ID Cir area or  "(Math.Pi * (FGMatODRad ^ 2 - FGMatIDRad ^ 2)" 

 

 

 

 

 

Dim FGCalculatedArea As Double = Math.PI * (FGMatODRad ^ 2 - FGMatIDRad ^ 2)
						
						Dim FGTolerance As Double = .01
				
						Dim PliesFaceCollection As FaceCollection = oTO.CreateFaceCollection
						Dim PliesEdgeCollection As EdgeCollection = oTO.CreateEdgeCollection
						
						For Each oFace As Face In PliesExt.Faces
							If Math.Abs(oFace.Evaluator.Area - FGCalculatedArea) < FGTolerance Then 
								PliesFaceCollection.Add(oFace)
							End If 
						Next
						
								Logger.Info("FG ID Rad: " & FGMatIDRad)
								Logger.Info("FG OD Rad: " & FGMatODRad)
								
								Logger.Info("FGcalculatedarea = " & FGCalculatedArea)
								Logger.Info("Plies face collection items: " & PliesFaceCollection.Count)
						
						For Each oFace As Face In PliesExt.Faces
							If Math.Abs(oFace.Evaluator.Area - FGCalculatedArea) < FGTolerance Then 
								For Each oEdge As Edge In oFace.Edges
										Dim Max As Double = Nothing
										Dim Min As Double = Nothing
										oEdge.Evaluator.GetParamExtents(Min, Max)
										Dim Length = Max - Min 
										
											Logger.Info("Min: " & Min)
											Logger.Info("Max: " & Max)
											Logger.Info("Length: " & Length)
											
										If Length = "some known edge length value" Then 
											'oEdgecollection.add(oEdge) add the edge to the collection
										End If 
								Next
							End If 
						Next
								
						
						PliesEdgeCollection.Add(PliesFaceCollection.Item(1).Edges.Item(2))
						PliesEdgeCollection.Add(PliesFaceCollection.Item(2).Edges.Item(2))
						
						Dim PliesChamfer As ChamferFeature = oCompDef.Features.ChamferFeatures.AddUsingDistance(PliesEdgeCollection, .1875 * 2.54)
						PliesChamfer.Name = "Matting chamfer feature: " & Fitting_Index

 

 

 

 

You might notice but when I target edges I typically use the face to find the edge collection, then find the edges manually by logging. Loading in the exact names or identifiers of that edge. I should start making this more dynamic in my own code. 

0 Likes
Message 3 of 3

Mehmet_Fatih_Turker
Advocate
Advocate

@J_Pfeifer_  Thank you for the idea you gave me. I updated mine regarding that approach 

Sub Main()

    Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
    Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
    Dim oNamedEntities As NamedEntities = iLogicVb.Automation.GetNamedEntities(oPDoc)
    Dim Highlighter As HighlightSet = oPDoc.CreateHighlightSet()

    Dim yzPlaneCrossing As Boolean
    Dim xzPlaneCrossing As Boolean
	
    Dim faceCounter As Integer = 1
    Dim edgeCounter As Integer = 1

    For Each oFace As Face In oPDef.SurfaceBodies.Item(1).Faces
		
        yzPlaneCrossing = False
        xzPlaneCrossing = False

        For Each oEdge As Edge In oFace.Edges
			
            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
				
                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)
				
            Catch ex As Exception
				
            End Try

            For Each oEdge As Edge In oFace.Edges
				
                Dim startVertex As Vertex = oEdge.StartVertex
                Dim stopVertex As Vertex = oEdge.StopVertex
                
                Dim startY As Double = startVertex.Point.Y
                Dim stopY As Double = stopVertex.Point.Y
                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)
                                            
					Catch ex As Exception
                        
					End Try
					
                    edgeCounter += 1
					
                End If
				
            Next

            faceCounter += 1
        End If
    Next

    MsgBox("Names assigned to faces and edges crossing the YZ plane but not the XZ plane.")

 

Seems like GPT a bit of weak at this point. So I taught the GPT, he will be more consistent next time 🙂