I need to locate the origin of a component using C#. I though about using bounding box but that is not close enough. Is there an easy way to get these coordinates?
SolidworksApi macros
I need to locate the origin of a component using C#. I though about using bounding box but that is not close enough. Is there an easy way to get these coordinates?
SolidworksApi macros
I'm not familiar with the C# syntax but I can tell you what I know about the API.
Every component has the property "Transform2"
2012 SOLIDWORKS API Help - Transform2 Property (IComponent2)
This is an affine transform (MathTransform type) from the root component to the component in question. It will contain the x, y, and z displacements along with a 3x3 rotation matrix and scaling factor.
[r11, r12, r13, 0]
[r21, r22, r23, 0]
[r31, r32, r33, 0]
[dx, dy, dz, scale]
scale is normally 1
You can access these values with MathTransform's ArrayData property which is organize as follows:
0, 1, 2, 3
4, 5, 6, 7
8, 9, 10, 11
12, 13, 14, 15
dx = Component.Transform2.ArrayData(12)
dy = Component.Transform2.ArrayData(13)
dz = Component.Transform2.ArrayData(14)
Because the value is reported with respect to the root component, you need to start with the root component of the assembly and get a reference to the desired component using the GetChildren method.
I need to make a correction to this - it actually just caused me issues with a macro that I wrote. I was trying to build an identity matrix and noticed that it was translating the result by 1000mm along the Y axis.
the indices of the MathTransform are actually (from the solidworks help page):
|a b c . n |
|d e f . o |
|g h i . p |
|------. --|
|j k l . m |
-or-
0 1 2 13
3 4 5 14
6 7 8 15
9 10 11 12
Here is what I ended up using.
internal double[] getOriginPointInfo(Component2 comp, ModelDoc2 swDoc)
{
double[] mPT = null;
{
double[, ,] pt = new double[0, 0, 0];
SelectionMgr swSelMan = (SelectionMgr)swDoc.SelectionManager;
object selOrigin = (object)swSelMan.GetSelectedObject6(0, -1);
MathUtility math = (MathUtility)_swApp.GetMathUtility();
MathTransform trany = comp.Transform2;
MathPoint mPoint = (MathPoint)math.CreatePoint(pt);
MathPoint tranPoint = (MathPoint)mPoint.IMultiplyTransform(trany);
mPT = (double[])tranPoint.ArrayData;
}
return mPT;
}