C# SolidWorks 二次开发 API—钣金零件信息提取
有一段时间没有发博文了,最近有人问到钣金件的相关操作,今天我们来看看钣金零件信息的读取。
Solidworks中钣金件是有一个单独的模块,里面有一些固定的文件夹结构。
最后这个文件夹中包含了展开状态时零件的信息。如长度与折弯线相关的信息都在这里面。
一般我们要找到长 宽 厚 折弯角 与距离 折弯方向 这些信息。
输出结果如下,有些小数的处理就忽略了。大家看看就行。
粗略的写了下,输入信息在这里。很多就是API帮助里面的例子,大家可以在中文翻译中查找到。
private void btnSheetmetal_Click(object sender, EventArgs e)
{
//连接到Solidworks
ISldWorks swApp = Utility.ConnectToSolidWorks();
swApp.CommandInProgress = true;
ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
//钣金 变成平板模式的特征
List<Feature> flatPatternFeatures = new List<Feature>();
//Bounding Box草图
List<string> boundingSketchesName = new List<string>();
//获取当前钣金状态--这个已经过时
//swSMBendStateFlattened 2 = 弯曲变平;该模型回滚到FlattenBends功能之后,但恰好在相应的ProcessBends功能之前
//swSMBendStateFolded 3 = 折弯处已折叠;模型回滚到FlattenBends ProcessBends功能对之后
//swSMBendStateNone 0 = 不是钣金零件;没有SheetMetal功能
//swSMBendStateSharps 1 = 弯曲处处于锐利状态;零件回滚到第一个FlattenBends功能之前
var bendState = swModel.GetBendState();
if (bendState == 0)
{
swApp.SendMsgToUser("不是钣金零件!");
return;
}
// swApp.SendMsgToUser("当前状态" + bendState);
if (bendState != 2)
{
//swApp.Command((int)swCommands_e.swCommands_Flatten, "");
//设定当前钣金状态 平板 ,下面这行代码不适用现在的零件 ,只适用于很早之前的零件
//var setStatus = swModel.SetBendState((int)swSMBendState_e.swSMBendStateFlattened);
//新钣金均是通过获取零件
var swFeatureManager = swModel.FeatureManager;
var flatPatternFolder = (FlatPatternFolder)swFeatureManager.GetFlatPatternFolder();
var featArray = (object[])flatPatternFolder.GetFlatPatterns();
for (int i = featArray.GetLowerBound(0); i <= featArray.GetUpperBound(0); i++)
{
var feat = (Feature)featArray[i];
Debug.Print(" " + feat.Name);
flatPatternFeatures.Add(feat);
feat.SetSuppression2((int)swFeatureSuppressionAction_e.swUnSuppressFeature, (int)swInConfigurationOpts_e.swThisConfiguration, null);
//解压子特征
var swSubFeat = (Feature)feat.GetFirstSubFeature();
while ((swSubFeat != null))
{
Debug.Print(swSubFeat.Name.ToString());
switch (swSubFeat.GetTypeName())
{
//如果是草图
case "ProfileFeature":
var sketchSpc = (Sketch)swSubFeat.GetSpecificFeature2();
object[] vSketchSeg = sketchSpc.GetSketchSegments();
for (int j = 0; j < vSketchSeg.Length; j++)
{
SketchSegment swSketchSeg = (SketchSegment)vSketchSeg[j];
//如果直线不是折弯线,说明是边界框
if (swSketchSeg.IsBendLine() == false)
{
boundingSketchesName.Add(swSubFeat.Name);
}
else if (swSketchSeg.IsBendLine() == true)
{
Debug.Print("钣金宽度为:" + swSketchSeg.GetLength() * 1000);
}
}
break;
default:
break;
}
swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
}
}
swModel.EditRebuild3();
}
//遍历所有特征
var swSelMgr = (SelectionMgr)swModel.SelectionManager;
var swFeat = (Feature)swModel.FirstFeature();
while ((swFeat != null))
{
//Debug.Print(swFeat.Name.ToString());
// Process top-level sheet metal features
switch (swFeat.GetTypeName())
{
case "SMBaseFlange":
//var swBaseFlange = (BaseFlangeFeatureData)swFeat.GetDefinition();
//Debug.Print("钣金宽度为:" + swBaseFlange.D1OffsetDistance * 1000);
break;
case "SheetMetal":
//这里可以获取默认的厚度 Debug.Print(swFeat.Name.ToString());
SheetMetalFeatureData sheetMetalFeatureData = (SheetMetalFeatureData)swFeat.GetDefinition();
Debug.Print("钣金默认厚度为:" + sheetMetalFeatureData.Thickness * 1000);
break;
case "SM3dBend":
break;
case "SMMiteredFlange":
break;
}
// process sheet metal sub-features
var swSubFeat = (Feature)swFeat.GetFirstSubFeature();
while ((swSubFeat != null))
{
// Debug.Print(swSubFeat.Name.ToString());
switch (swSubFeat.GetTypeName())
{
case "SketchBend":
GetHisBendInformation(swApp, swModel, swSubFeat);
break;
case "OneBend":
GetHisBendInformation(swApp, swModel, swSubFeat);
break;
default:
break;
// Probably not a sheet metal feature
}
swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
}
swFeat = (Feature)swFeat.GetNextFeature();
}
return;
}
private void GetHisBendInformation(ISldWorks swApp, ModelDoc2 swModel, Feature swFeat)
{
MathUtility swMathUtil = default(MathUtility);
SelectionMgr swSelMgr = default(SelectionMgr);
OneBendFeatureData swOneBend = default(OneBendFeatureData);
Object[] vSketchSegs = null;
SketchSegment swSketchSeg = default(SketchSegment);
Sketch swSketch = default(Sketch);
Feature swSketchFeat = default(Feature);
SketchLine swSketchLine = default(SketchLine);
SketchPoint swSkStartPt = default(SketchPoint);
SketchPoint swSkEndPt = default(SketchPoint);
SelectData swSelData = default(SelectData);
double[] nPt = new double[3];
MathPoint swStartPt = default(MathPoint);
MathPoint swEndPt = default(MathPoint);
MathTransform swSkXform = default(MathTransform);
int[] vID = null;
int i = 0;
swMathUtil = (MathUtility)swApp.GetMathUtility();
swSelMgr = (SelectionMgr)swModel.SelectionManager;
//swFeat = (Feature)swSelMgr.GetSelectedObject6(1, -1);
//swSelData = swSelMgr.CreateSelectData();
swOneBend = (OneBendFeatureData)swFeat.GetDefinition();
/*swBaseBend 4
swEdgeFlangeBend 8
swFlat3dBend 6
swFlatBend 2
swFreeFormBend 10 = Obsolete
swHemBend 9
swLoftedBend 12
swMirrorBend 7
swMiterBend 5
swNoneBend 3
swRoundBend 1
swRuledBend 11 = Obsolete
swSharpBend 0
*/
Debug.Print("Type of bend (swBendType_e): " + swOneBend.GetType());
Debug.Print("折弯次数: " + swOneBend.GetFlatPatternSketchSegmentCount2());
Debug.Print("折弯序号: " + swOneBend.BendOrder);
Debug.Print("折弯角度: " + swOneBend.BendAngle * 57.3 + " deg");
Debug.Print("折弯圆角: " + swOneBend.BendRadius);
if (swOneBend.BendDown == true)
{
Debug.Print("向下折弯: " + "Yes");
}
else
{
Debug.Print("向下折弯: " + " No");
}
vSketchSegs = (Object[])swOneBend.FlatPatternSketchSegments2;
for (i = 0; i <= vSketchSegs.GetUpperBound(0); i++)
{
swSketchSeg = (SketchSegment)vSketchSegs[i];
swSketch = swSketchSeg.GetSketch();
swSketchLine = (SketchLine)swSketchSeg;
swSkStartPt = (SketchPoint)swSketchLine.GetStartPoint2();
swSkEndPt = (SketchPoint)swSketchLine.GetEndPoint2();
vID = (int[])swSketchSeg.GetID();
// Get sketch feature
swSketchFeat = (Feature)swSketch;
swSkXform = swSketch.ModelToSketchTransform;
swSkXform = (MathTransform)swSkXform.Inverse();
nPt[0] = swSkStartPt.X;
nPt[1] = swSkStartPt.Y;
nPt[2] = swSkStartPt.Z;
swStartPt = (MathPoint)swMathUtil.CreatePoint(nPt);
swStartPt = (MathPoint)swStartPt.MultiplyTransform(swSkXform);
double[] swStartPtArrayData;
swStartPtArrayData = (double[])swStartPt.ArrayData;
nPt[0] = swSkEndPt.X;
nPt[1] = swSkEndPt.Y;
nPt[2] = swSkEndPt.Z;
swEndPt = (MathPoint)swMathUtil.CreatePoint(nPt);
swEndPt = (MathPoint)swEndPt.MultiplyTransform(swSkXform);
double[] swEndPtArrayData;
swEndPtArrayData = (double[])swEndPt.ArrayData;
// Debug.Print("File = " + swModel.GetPathName());
Debug.Print(" Feature = " + swFeat.Name + " [" + swFeat.GetTypeName2() + "]");
Debug.Print(" Sketch = " + swSketchFeat.Name);
Debug.Print(" SegID = [" + vID[0] + ", " + vID[1] + "]");
Debug.Print(" Start with respect to sketch = (" + swSkStartPt.X * 1000.0 + ", " + swSkStartPt.Y * 1000.0 + ", " + swSkStartPt.Z * 1000.0 + ") mm");
Debug.Print(" End with respect to sketch = (" + swSkEndPt.X * 1000.0 + ", " + swSkEndPt.Y * 1000.0 + ", " + swSkEndPt.Z * 1000.0 + ") mm");
Debug.Print(" Start with respect to model = (" + swStartPtArrayData[0] * 1000.0 + ", " + swStartPtArrayData[1] * 1000.0 + ", " + swStartPtArrayData[2] * 1000.0 + ") mm");
Debug.Print(" End with respect to model = (" + swEndPtArrayData[0] * 1000.0 + ", " + swEndPtArrayData[1] * 1000.0 + ", " + swEndPtArrayData[2] * 1000.0 + ") mm");
}
}
posted @
2020-04-15 10:53
painezeng 阅读(
410) 评论(
0)
编辑
收藏
举报