// 获取最大值
public double GetMax(double Val1, double Val2, double Val3)
{
double functionReturnValue = 0;
// Finds maximum of 3 values
functionReturnValue = Val1;
if (Val2 > functionReturnValue)
{
functionReturnValue = Val2;
}
if (Val3 > functionReturnValue)
{
functionReturnValue = Val3;
}
return functionReturnValue;
}
// 获取最小值
public double GetMin(double Val1, double Val2, double Val3)
{
double functionReturnValue = 0;
// Finds minimum of 3 values
functionReturnValue = Val1;
if (Val2 < functionReturnValue)
{
functionReturnValue = Val2;
}
if (Val3 < functionReturnValue)
{
functionReturnValue = Val3;
}
return functionReturnValue;
}
// 获取装配体长宽高
public void GetXYZ(Component2 swComponent, out double X, out double Y, out double Z)
{
const double MaxDouble = 1.79769313486231E+308;
const double MinDouble = -1.79769313486231E+308;
object[] vChild = null;
Component2 swChildComp = default(Component2);
object Box = null;
double[] BoxArray = new double[6];
double X_max = 0;
double X_min = 0;
double Y_max = 0;
double Y_min = 0;
double Z_max = 0;
double Z_min = 0;
// Initialize values
X_max = MinDouble;
X_min = MaxDouble;
Y_max = MinDouble;
Y_min = MaxDouble;
Z_max = MinDouble;
Z_min = MaxDouble;
vChild = (object[])swComponent.GetChildren();
for (int i = 0; i <= vChild.GetUpperBound(0); i++)
{
swChildComp = (Component2)vChild[i];
if (swChildComp.Visible == (int)swComponentVisibilityState_e.swComponentVisible)
{
Box = (object)swChildComp.GetBox(false, false);
if (Box != System.DBNull.Value)
{
BoxArray = (double[])Box;
X_max = GetMax(BoxArray[0], BoxArray[3], X_max);
X_min = GetMin(BoxArray[0], BoxArray[3], X_min);
Y_max = GetMax(BoxArray[1], BoxArray[4], Y_max);
Y_min = GetMin(BoxArray[1], BoxArray[4], Y_min);
Z_max = GetMax(BoxArray[2], BoxArray[5], Z_max);
Z_min = GetMin(BoxArray[2], BoxArray[5], Z_min);
}
}
}
// 长、宽、高
// 将m转换为mm(四舍五入)
X = Math.Round((X_max - X_min)*1000);
Y = Math.Round((Y_max - Y_min)*1000);
Z = Math.Round((Z_max - Z_min)*1000);
}
///
/// 绘制装配体边界框
///
public void BoundingAssemblyBox()
{
const double MaxDouble = 1.79769313486231E+308;
const double MinDouble = -1.79769313486231E+308;
ModelDoc2 swModel = default(ModelDoc2);
AssemblyDoc swAssy = default(AssemblyDoc);
Configuration swConfig = default(Configuration);
ConfigurationManager swConfigurationMgr = default(ConfigurationManager);
Component2 swRootComp = default(Component2);
object[] vChild = null;
Component2 swChildComp = default(Component2);
object Box = null;
double[] BoxArray = new double[6];
double X_max = 0;
double X_min = 0;
double Y_max = 0;
double Y_min = 0;
double Z_max = 0;
double Z_min = 0;
SketchManager swSketchMgr = default(SketchManager);
SketchPoint[] swSketchPt = new SketchPoint[9];
SketchSegment[] swSketchSeg = new SketchSegment[13];
int i = 0;
if (iSwApp.GetDocumentCount() == 0)
{
MessageBox.Show("没有SolidWorks文件!", "提示信息");
return;
}
swModel = (ModelDoc2)iSwApp.ActiveDoc;
// 判断该活动文档是否为零部件
if (swModel.GetType() != (int)swDocumentTypes_e.swDocASSEMBLY)
{
MessageBox.Show("活动文档不是装配体,请选择装配体!", "提示信息");
return;
}
swAssy = (AssemblyDoc)swModel;
swConfigurationMgr = (ConfigurationManager)swModel.ConfigurationManager;
swConfig = (Configuration)swConfigurationMgr.ActiveConfiguration;
swRootComp = (Component2)swConfig.GetRootComponent3(true);
// Initialize values
X_max = MinDouble;
X_min = MaxDouble;
Y_max = MinDouble;
Y_min = MaxDouble;
Z_max = MinDouble;
Z_min = MaxDouble;
vChild = (object[])swRootComp.GetChildren();
for (i = 0; i <= vChild.GetUpperBound(0); i++)
{
swChildComp = (Component2)vChild[i];
if (swChildComp.Visible == (int)swComponentVisibilityState_e.swComponentVisible)
{
Box = (object)swChildComp.GetBox(false, false);
if (Box != System.DBNull.Value)
{
BoxArray = (double[])Box;
X_max = GetMax(BoxArray[0], BoxArray[3], X_max);
X_min = GetMin(BoxArray[0], BoxArray[3], X_min);
Y_max = GetMax(BoxArray[1], BoxArray[4], Y_max);
Y_min = GetMin(BoxArray[1], BoxArray[4], Y_min);
Z_max = GetMax(BoxArray[2], BoxArray[5], Z_max);
Z_min = GetMin(BoxArray[2], BoxArray[5], Z_min);
}
}
}
Debug.Print("Assembly Bounding Box (" + swModel.GetPathName() + ") = ");
Debug.Print(" (" + (X_min * 1000.0) + "," + (Y_min * 1000.0) + "," + (Z_min * 1000.0) + ") mm");
Debug.Print(" (" + (X_max * 1000.0) + "," + (Y_max * 1000.0) + "," + (Z_max * 1000.0) + ") mm");
swSketchMgr = swModel.SketchManager;
swSketchMgr.Insert3DSketch(true);
swSketchMgr.AddToDB = true;
// Draw points at each corner of bounding box
swSketchPt[0] = swSketchMgr.CreatePoint(X_min, Y_min, Z_min);
swSketchPt[1] = swSketchMgr.CreatePoint(X_min, Y_min, Z_max);
swSketchPt[2] = swSketchMgr.CreatePoint(X_min, Y_max, Z_min);
swSketchPt[3] = swSketchMgr.CreatePoint(X_min, Y_max, Z_max);
swSketchPt[4] = swSketchMgr.CreatePoint(X_max, Y_min, Z_min);
swSketchPt[5] = swSketchMgr.CreatePoint(X_max, Y_min, Z_max);
swSketchPt[6] = swSketchMgr.CreatePoint(X_max, Y_max, Z_min);
swSketchPt[7] = swSketchMgr.CreatePoint(X_max, Y_max, Z_max);
// Draw bounding box
swSketchSeg[0] = swSketchMgr.CreateLine(X_min, Y_min, Z_min, X_max, Y_min, Z_min);
swSketchSeg[1] = swSketchMgr.CreateLine(X_max, Y_min, Z_min, X_max, Y_min, Z_max);
swSketchSeg[2] = swSketchMgr.CreateLine(X_max, Y_min, Z_max, X_min, Y_min, Z_max);
swSketchSeg[3] = swSketchMgr.CreateLine(X_min, Y_min, Z_max, X_min, Y_min, Z_min);
swSketchSeg[4] = swSketchMgr.CreateLine(X_min, Y_min, Z_min, X_min, Y_max, Z_min);
swSketchSeg[5] = swSketchMgr.CreateLine(X_min, Y_min, Z_max, X_min, Y_max, Z_max);
swSketchSeg[6] = swSketchMgr.CreateLine(X_max, Y_min, Z_min, X_max, Y_max, Z_min);
swSketchSeg[7] = swSketchMgr.CreateLine(X_max, Y_min, Z_max, X_max, Y_max, Z_max);
swSketchSeg[8] = swSketchMgr.CreateLine(X_min, Y_max, Z_min, X_max, Y_max, Z_min);
swSketchSeg[9] = swSketchMgr.CreateLine(X_max, Y_max, Z_min, X_max, Y_max, Z_max);
swSketchSeg[10] = swSketchMgr.CreateLine(X_max, Y_max, Z_max, X_min, Y_max, Z_max);
swSketchSeg[11] = swSketchMgr.CreateLine(X_min, Y_max, Z_max, X_min, Y_max, Z_min);
swSketchMgr.AddToDB = false;
swSketchMgr.Insert3DSketch(true);
}