I would like to create a VBA macro, which is partly already working. I can select a face in my part by a mouse selection.
I already searched for the solution to get a selected faces center point coordinates. I would like to create a sketch with CreateCenterRectangle with the center point of the selected face.
The problem is that I cant really get the center point coordinates.
I tried with this code:
Public Function getFaceCenter()
Set swFace = swModel.SelectionManager.GetSelectedObject6(1, -1)
GetFaceCenterParameters swFace, vPt, vNorm
Public Function GetFaceCenterParameters(face As SldWorks.Face2, ByRef point As Variant, ByRef normal As Variant)
vUvBounds = face.GetUVBounds
centerU = (vUvBounds(0) + vUvBounds(1)) / 2
centerV = (vUvBounds(2) + vUvBounds(3)) / 2
Set swSurf = face.GetSurface
vEvalRes = swSurf.Evaluate(centerU, centerV, 0, 0)
dPoint(0) = vEvalRes(0)
dPoint(1) = vEvalRes(1)
dPoint(2) = vEvalRes(2)
dNormal(0) = vEvalRes(3)
dNormal(1) = vEvalRes(4)
dNormal(2) = vEvalRes(5)
point = dPoint
normal = dNormal
Then I put the coordinates here:
vSkLines = Part.SketchManager.CreateCenterRectangle(vPt(0), vPt(1), vPt(2), ....
If I'm correct, then the CreateCenterRectangles first three coordinates are the center point.
I already tried to use a coordinate transformation, but I dont really understand it and it didnt worked as well.
Tried it with this:
Set swMathUtil = swApp.GetMathUtility
Set swMathPoint = swMathUtil.CreatePoint(vPtArr)
Set Sketch = swModelDoc.GetActiveSketch2
Set ModelToSketchTransform = Sketch.ModelToSketchTransform
Set swMathPoint = swMathPoint.MultiplyTransform(ModelToSketchTransform)
Dim vModelSelPt As Variant
vModelSelPt = swMathPoint.ArrayData
Tried it with the ModelToSketchTransform.Invert too.
If I put centroid coordinates manually from the evaluation it wont be in the center too.
I would really appriciate your help.
Thank you very much in advance.