I am trying to write a macro which will place a sketch point at the centroid of a section. The code is below. To use it create a closed section on a 2D sketch plane and measure the section properties. Then execute the macro.
The macro works, but the problem is the point it places is not exactly at the same coordinates as the centroid of the section. The code prints out in an Immediate Window with the coordinates. These are correct. The point it places, however, is not at these coordinates.
Example, here is a section:
Here is the results of "Evaluate->Section Properties":
Here is the results in the Immediate Window after running the macro:
Note the coordinate are correct to within six decimal places.
Here is the display after the macro is run:
Here is where Solidworks says that new point is:
Note the X value is right on, but the Z value is off.
Sketch coordinates are similar:
What am I doing wrong?!?!
As a secondary problem the midpoint of those two lines do not line up with the sketch point, even though they are both created with that point being in the center.
And...
Any help anybody could give me would be greatly appreciated!!!
Here is the macro code:
' This macro places a point at the centroid of a section to allow for easily measuring the distance
' from the centroid to some point on the section (the "c" in "Mc/I").
' It also places two construction lines aligned with the principal axis of the section.
' To use, first use the "Section Properties" command under "Evaluate".
' Then just execute the macro.
Sub main()
Dim swApp As SldWorks.SldWorks
Dim myModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swSectionProps() As Double
Dim skPoint As Object
Dim skSegment As Object
Dim vSkContours As Variant
Dim skContour As SketchContour
Dim SelMgr As SldWorks.SelectionMgr
Dim mySelectData As SldWorks.SelectData
Dim mySketch As SldWorks.Sketch
Dim CentroidID As Variant
Dim CentroidXID As Variant
Dim CentroidYID As Variant
Dim swTransform As SldWorks.MathTransform
Dim swMathUtils As SldWorks.MathUtility
Dim CentroidPt(2) As Double
Dim swMathPt As SldWorks.MathPoint
Dim SktchCentroidPt As Variant
Set swApp = Application.SldWorks
Set myModel = swApp.ActiveDoc
Set SelMgr = myModel.SelectionManager
Set mySelectData = SelMgr.CreateSelectData
Set swModelDocExt = myModel.Extension
Set mySketch = myModel.GetActiveSketch2()
Set swTransform = mySketch.ModelToSketchTransform
Set swMathUtils = swApp.GetMathUtility
'Get the contours in the sketch (there should only be one)
vSkContours = mySketch.GetSketchContours()
Set skContour = vSkContours(0)
'This gets all the section properties
swSectionProps = myModel.Extension.GetSectionProperties2(skContour)
'Creates a point at the centroid
'Create the coordinates for the point in model coordinate system
CentroidPt(0) = swSectionProps(2)
CentroidPt(1) = swSectionProps(3)
CentroidPt(2) = swSectionProps(4)
'Transform this point into the sketch coordiante system
'Set swMathPt = swMathUtils.CreatePoint(CentroidPt)
Set swMathPt = swMathUtils.CreatePoint(CentroidPt).MultiplyTransform(swTransform)
SktchCentroidPt = swMathPt.ArrayData
Set skPoint = myModel.SketchManager.CreatePoint(SktchCentroidPt(0), SktchCentroidPt(1), SktchCentroidPt(2))
'CentroidID = skPoint.GetID
Debug.Print ("Sketch Centroid X = " & SktchCentroidPt(0) * 39.37007874)
Debug.Print ("Sketch Centroid Y = " & SktchCentroidPt(1) * 39.37007874)
Debug.Print ("Sketch Centroid Z = " & SktchCentroidPt(2) * 39.37007874)
Debug.Print ("")
' Create a line aligned with the x-axis of the sketch
Set skSegment = myModel.SketchManager.CreateLine(SktchCentroidPt(0) - 0.0064, SktchCentroidPt(1), SktchCentroidPt(2), SktchCentroidPt(0) + 0.0064, SktchCentroidPt(1), SktchCentroidPt(2))
' Get Name of Line Segment
CentroidXID = skSegment.GetID
'Convert segment into construction geometry
skSegment.ConstructionGeometry = True
'Rotate line to align with principal axis
swModelDocExt.RotateOrCopy False, 1, False, SktchCentroidPt(0), SktchCentroidPt(1), SktchCentroidPt(2), 0, 0, 1, swSectionProps(12)
' Create a line aligned the with y-axis of the sketch
Set skSegment = myModel.SketchManager.CreateLine(SktchCentroidPt(0), SktchCentroidPt(1) - 0.0064, SktchCentroidPt(2), SktchCentroidPt(0), SktchCentroidPt(1) + 0.0064, SktchCentroidPt(2))
' Get Name of Line Segment
CentroidYID = skSegment.GetID
'Convert segment into construction geometry
skSegment.ConstructionGeometry = True
'Rotate line to align with principal axis
swModelDocExt.RotateOrCopy False, 1, False, SktchCentroidPt(0), SktchCentroidPt(1), SktchCentroidPt(2), 0, 0, 1, swSectionProps(12)
'Deselect everything
myModel.ClearSelection2 True
End Sub