Question:
In a assembly (or part), I want to select a face, on this face I want the Partnumber as a sketh text, (with a big font)
Is there a simple solution?
Answer:
The code below assumes a face of assembly is selected, and a planar sketch will be created on the face, and a text will be added on the location of the center point of the face area.
VBA code:
Sub addTextToFace()
Dim oAssDoc As AssemblyDocument
Set oAssDoc = ThisApplication.ActiveDocument
Dim oAssDef As AssemblyComponentDefinition
Set oAssDef = oAssDoc.ComponentDefinition
'assume a face has been selected in the context of assembly
Dim oFace As FaceProxy
Set oFace = oAssDoc.SelectSet(1)
'make sure it is a planar face
If oFace.SurfaceType = kPlaneSurface Then
Dim oPlanarSurface As Plane
Set oPlanarSurface = oFace.Geometry
Dim oSketch As PlanarSketch
Set oSketch = oAssDef.Sketches.Add(oFace)
'trying to choose an appropriate point
'assume this planar face has one edge loop only
Dim oEdgeLoop As EdgeLoop
Set oEdgeLoop = oFace.EdgeLoops(1)
Dim oMinPt As Point
Set oMinPt = oEdgeLoop.RangeBox.MinPoint
Dim oMaxPt As Point
Set oMaxPt = oEdgeLoop.RangeBox.MaxPoint
Dim oCenterPt As Point
Set CenterPt = ThisApplication.TransientGeometry.CreatePoint((oMaxPt.X + oMinPt.X) / 2#, (oMaxPt.Y + oMinPt.Y) / 2#, (oMaxPt.Z + oMinPt.Z) / 2#)
'get one point on the face and transform to the point2d on the sketch
Dim oTextPt As Point2d
'Set oTextPt = oSketch.ModelToSketchSpace(oPlanarSurface.RootPoint)
Set oTextPt = oSketch.ModelToSketchSpace(CenterPt)
'add the textbox
Dim oSketchText As TextBox
Set oSketchText = oSketch.TextBoxes.AddFitted(oTextPt, "MY TEST")
Else
MsgBox "please select a planar face!"
End If
End Sub
iLogic code:
oAssDoc = ThisApplication.ActiveDocument
oAssDef = oAssDoc.ComponentDefinition
If oAssDoc.SelectSet.Count = 0 Then
MsgBox("no face is selected!")
Else
'assume a face has been selected in the context of assembly
oFace = oAssDoc.SelectSet(1)
'make sure it is a planar face
If oFace.SurfaceType = SurfaceTypeEnum.kPlaneSurface Then
oPlanarSurface = oFace.Geometry
'add a sketch
oSketch = oAssDef.Sketches.Add(oFace)
'trying to choose an appropriate point
'assume this planar face has one edge loop only
oEdgeLoop = oFace.EdgeLoops(1)
oMinPt = oEdgeLoop.RangeBox.MinPoint
oMaxPt = oEdgeLoop.RangeBox.MaxPoint
CenterPt = ThisApplication.TransientGeometry.CreatePoint((oMaxPt.X + oMinPt.X) / 2#, (oMaxPt.Y + oMinPt.Y) / 2#, (oMaxPt.Z + oMinPt.Z) / 2#)
'get one point on the face and transform to the point2d on the sketch
'Set oTextPt = oSketch.ModelToSketchSpace(oPlanarSurface.RootPoint)
oTextPt = oSketch.ModelToSketchSpace(CenterPt)
'add the textbox
oSketchText = oSketch.TextBoxes.AddFitted(oTextPt, "MY TEST")
Else
MsgBox( "please select a planar face!")
End If
End If