Good day. There is a detail with a hole. Tell me how to get the coordinates of this hole relative to the beginning of the coordinates of the part?
Good day. There is a detail with a hole. Tell me how to get the coordinates of this hole relative to the beginning of the coordinates of the part?
Thank you very much. I am writing in C #. But everything is very similar. Be sure to read this blog.
Your welcome!!!
Yes you are right. Most of this thing are similar but in C# we need to "Cast" everything.
Sometimes I missed casting of "Constants" into "int" which cause me issues!!!
Hopefully you don't have to face such issues.
If you get what you are looking for please mark it as answered.
I give the code for obtaining the coordinates of the hole.
if (TypFeat == "HoleWzd")
{
OtvName = swFeat.Name;
swModelDocExt = (ModelDocExtension)swModelPart.Extension;
boolstatus = swModelDocExt.SelectByID2(OtvName, "BODYFEATURE", 0, 0, 0, false, 0, null, 0);
WizardHoleFeatureData2 swWizardHoleFeatureData = default(WizardHoleFeatureData2);
swWizardHoleFeatureData = (WizardHoleFeatureData2)swFeat.GetDefinition();
int countt = swWizardHoleFeatureData.GetSketchPointCount();
object[] points = null;
points = (object[])swWizardHoleFeatureData.GetSketchPoints();
for (int q = 0; q < points.Length; q++) <<<<<<<<<<<<<<<<<<< Length is always 1
{
SketchPoint coardPoint = (SketchPoint)points[q];
double coard = coardPoint.GetCoords();
}
}
In this way, you can get only one coordinate X. And how to get Y and Z?
As per document "GetCoords()" will get you "X" co-ordinates only.
To get all three co-ordinates you need to use properties of the point.
I have modified your code to get x, y and z coordinates.
// Loop to iterate through every point
for (int q = 0; q < points.Length; q++)
{
// Variable of Sketch Point type
SketchPoint coardPoint = (SketchPoint)points[q];
// X Coordinate of point
double Xcoard = coardPoint.X;
// Y Coordinate of point
double Ycoard = coardPoint.Y;
// Z Coordinate of point
double Zcoard = coardPoint.Z;
}
Above code, will provide you with all 3 coordinates of a point in C#.
Hope this will get you what you are looking for.
Thanks
Prashant Baher
Hi,
You do not need to call these lines as you already have a pointer to the feature:
swModelDocExt = (ModelDocExtension)swModelPart.Extension;
boolstatus = swModelDocExt.SelectByID2(OtvName, "BODYFEATURE", 0, 0, 0, false, 0, null, 0);
You can use X,Y,Z properties of the SketchPoint to get the coordinates:
var pt = new double[] { coardPoint.X, coardPoint.Y, coardPoint.Z };
The GetCoords function is not correctly represented in the Interop for .NET and it is used in the managed code (like C++) where array is returned as a pointer to first element. It cannot be used in C#/VB.NET
Also note that the coordinates of the points (if those are in 2D sketch) need to be transformed to global CS otherwise all Z will be 0.
I express my deep gratitude to everyone. The issue is resolved. If you need someone, I quote the code.
TypFeat = swFeat.GetTypeName2();
if (TypFeat == "HoleWzd")
{
OtvName = swFeat.Name;// Получение имени отверстия
WizardHoleFeatureData2 swWizardHoleFeatureData = default(WizardHoleFeatureData2);
swWizardHoleFeatureData = (WizardHoleFeatureData2)swFeat.GetDefinition();
int countt = swWizardHoleFeatureData.GetSketchPointCount();
object[] points = null;
points = (object[])swWizardHoleFeatureData.GetSketchPoints();
swWizardHoleFeatureData.IGetSketchPoints(countt);
for (int q = 0; q < points.Length; q++)
{
SketchPoint coardPoint = (SketchPoint)points[q];
Sketch swSketch = coardPoint.GetSketch();
MathTransform swTransform = swSketch.ModelToSketchTransform.Inverse();
MathUtility swMathUtils = swApp.GetMathUtility();
double[] Koard = new double[] { coardPoint.X, coardPoint.Y, coardPoint.Z };
MathPoint swMathPt = swMathUtils.CreatePoint(Koard);
swMathPt = swMathPt.MultiplyTransform(swTransform);
dynamic vPt = swMathPt.ArrayData;
double KoardX = vPt[0] * 1000;
double KoardY = vPt[1] * 1000;
double KoardZ = vPt[2] * 1000;
}
}
Coordinates of the hole in the global system of coordinates are obtained.
Please tell me why the properties for swWizardHoleFeatureData.Type (swHoleBlind) give out 0.
double Diam = swWizardHoleFeatureData.ThruHoleDiameter * 1000; <<<<<< 0
double Diam1 = swWizardHoleFeatureData.ThruTapDrillDiameter * 1000; <<<<<< 0
double Diam2 = swWizardHoleFeatureData.TapDrillDiameter * 1000; <<<<<< 0
double Diam3 = swWizardHoleFeatureData.ThreadDiameter * 1000; <<<<<< 0
double Glub = swWizardHoleFeatureData.ThruHoleDepth * 1000; <<<<<< 0
double Glub1 = swWizardHoleFeatureData.ThruTapDrillDepth * 1000; <<<<<< 0
double Glub2 = swWizardHoleFeatureData.TapDrillDepth * 1000; <<<<<< 0
double Glub3 = swWizardHoleFeatureData.ThreadDepth * 1000; <<<<<< 0
Well I don't have any macro for this but I can provide some logical steps to create it.
1. Select that hole by "SelectByID2" Method
2. Check its feature type by "GetDefinition" of the selected "Feature"
3. If feature type = "IWizardHoleFeatureData2" then get sketch point locations from "GetSketchPoints" method.
2018 SOLIDWORKS API Help - IGetSketchPoints Method (IWizardHoleFeatureData2) <- This method will give you what you are looking for.
I have attached a link of blog which I found for VBA tutorials.
In case you don't know much about Solidworks API and VBA programming do visit that blog it might help you.