hi the following rue is not perfect. it expects that the biggest plane is parallel to on of the normal planes and that the biggest side is parallel to one of the normal axes.
(If your parts don't comply with that it would also be possible to do this wit the longest edge and an other point but that origin would not be on a corner of your part but at the start or end of the longest edge. )
Dim doc As PartDocument = ThisDoc.Document
Dim body As SurfaceBody = doc.ComponentDefinition.SurfaceBodies.Item(1)
Dim maxArea As Double = 0
Dim face As Face = Nothing
For Each selectedFace As face In body.Faces
Dim area As Double = selectedFace.Evaluator.Area
If (maxArea < area) Then
maxArea = area
face = selectedFace
End If
Next
Dim oCompDef As PartComponentDefinition = doc.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim orign As Point = face.Evaluator.RangeBox.MinPoint
Dim maxPoint As Point = face.Evaluator.RangeBox.MaxPoint
Dim diffX = maxPoint.X - orign.X
Dim diffY = maxPoint.Y - orign.Y
Dim diffZ = maxPoint.Z - orign.Z
' find max direction (= x direction)
Dim point2 As Point
If (diffX > diffY And diffX > diffZ) Then
point2 = oTG.CreatePoint(orign.X + diffX, orign.Y, orign.Z)
ElseIf (diffY > diffX And diffY > diffZ) Then
point2 = oTG.CreatePoint(orign.X, orign.Y + diffY, orign.Z)
Else
point2 = oTG.CreatePoint(orign.X, orign.Y, orign.Z + diffZ)
End If
' find small direction (= Z direction)
Dim point3 As Point
If (diffX < diffY And diffX < diffZ) Then
point3 = oTG.CreatePoint(orign.X, orign.Y + diffY, orign.Z + diffZ)
ElseIf (diffY < diffX And diffY < diffZ) Then
point3 = oTG.CreatePoint(orign.X + diffX, orign.Y, orign.Z + diffZ)
Else
point3 = oTG.CreatePoint(orign.X + diffX, orign.Y + diffY, orign.Z)
End If
Dim oWorkPoint1 As WorkPoint = oCompDef.WorkPoints.AddFixed(orign)
Dim oWorkPoint2 As WorkPoint = oCompDef.WorkPoints.AddFixed(point2)
Dim oWorkPoint3 As WorkPoint = oCompDef.WorkPoints.AddFixed(point3)
Dim oUCSDef As UserCoordinateSystemDefinition = oCompDef.UserCoordinateSystems.CreateDefinition
oUCSDef.SetByThreePoints(oWorkPoint1, oWorkPoint2, oWorkPoint3)
Dim oUCS As UserCoordinateSystem = oCompDef.UserCoordinateSystems.Add(oUCSDef)
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog: hjalte.nl - github.com