c#solidworks 二次开发 工程图标边线法兰 折弯尺寸标注
谢谢各位佬,看大家的回答有很多启发,目前可以标出来
using netDxf.Entities;
using NPOI.SS.Formula.Functions;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls.WebParts;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Dimension = SolidWorks.Interop.sldworks.Dimension;
using Edge = SolidWorks.Interop.sldworks.Edge;
using View = SolidWorks.Interop.sldworks.View;
namespace solidgai
{
public static class sw方法
{
public static void 标折弯尺寸(ISldWorks SwApp)
{
ModelDoc2 swModel = (ModelDoc2)SwApp.ActiveDoc;
DrawingDoc swDrawing = (DrawingDoc)swModel;
Sheet sheet = (Sheet)swDrawing.GetCurrentSheet();
var swMathUtils = SwApp.IGetMathUtility();
string partname = Path.GetFileNameWithoutExtension(swModel.GetPathName());
var Views = (object[])sheet.GetViews();
foreach (var view2 in Views)
{
View view = (View)view2;
double Offset = 0.05;
if (view.GetName2().Contains("图"))
{
List<(Face, string, bool, double)> faces = new List<(Face, string, bool, double)>();
string viewname = view.GetName2();
Debug.WriteLine("视图名称为:" + viewname);
var swViewXform = (MathTransform)view.ModelToViewTransform;
var ViewTransformDATA = (double[])swViewXform.ArrayData;
Debug.WriteLine($":{Math.Round(ViewTransformDATA[0], 2)},{Math.Round(ViewTransformDATA[1], 2)},{Math.Round(ViewTransformDATA[2], 2)},{Math.Round(ViewTransformDATA[13], 2)}");
Debug.WriteLine($":{Math.Round(ViewTransformDATA[3], 2)},{Math.Round(ViewTransformDATA[4], 2)},{Math.Round(ViewTransformDATA[5], 2)},{Math.Round(ViewTransformDATA[14], 2)}");
Debug.WriteLine($":{Math.Round(ViewTransformDATA[6], 2)},{Math.Round(ViewTransformDATA[7], 2)},{Math.Round(ViewTransformDATA[8], 2)},{Math.Round(ViewTransformDATA[15], 2)}");
Debug.WriteLine($":{Math.Round(ViewTransformDATA[9], 2)},{Math.Round(ViewTransformDATA[10], 2)},{Math.Round(ViewTransformDATA[11], 2)},{Math.Round(ViewTransformDATA[12], 2)}");
var vBounds = (double[])view.GetOutline();
view.SetDisplayMode3(false, (int)swDisplayMode_e.swFACETED_HIDDEN_GREYED, false, true);
Console.WriteLine("view.GetDisplayMode()=" + view.GetDisplayMode());
swModel.EditRebuild3();
PartDoc partDoc = (PartDoc)view.ReferencedDocument;
object[] vBodies = (object[])partDoc.GetBodies2((int)swBodyType_e.swSolidBody, false);
Body2 body = (Body2)vBodies[0];
object[] vAffectedFaces = (object[])body.GetFaces();
List<(Face, string, double, Edge)> 面集 = vAffectedFaces
.Where(item =>
{
Surface plansurface = (Surface)((Face)item).GetSurface();
object[] edges = (object[])((Face)item).GetEdges();
return plansurface.IsPlane() && ((Face)item).GetArea() * 1000000 > 10 && edges.Length > 3;
})
.Select(item =>
{
Surface plansurface = (Surface)((Face)item).GetSurface();
double[] Params = (double[])plansurface.PlaneParams;
(double, double, double) 平面朝向 = (Math.Round(Params[0]), Math.Round(Params[1]), Math.Round(Params[2]));
string 方向 = "";
double 位置值 = 0;
object[] edges = (object[])((Face)item).GetEdges();
Debug.WriteLine("edges.count=" + edges.Length + ",面积为=" + ((Face)item).GetArea() * 1000000 + $"平面朝向={平面朝向.Item1},{平面朝向.Item2},{平面朝向.Item3}");
Edge eedge = (Edge)edges[0];
Vertex StartVertex = (Vertex)eedge.GetStartVertex();
double[] pParams = (double[])StartVertex.GetPoint();
if (平面朝向.Item1 != 0)
{
位置值 = Math.Round((double)pParams[0] * 1000, 1);
方向 = "x";
}
else if (平面朝向.Item2 != 0)
{
位置值 = Math.Round((double)pParams[1] * 1000, 1);
方向 = "y";
}
else if (平面朝向.Item3 != 0)
{
位置值 = Math.Round((double)pParams[2] * 1000, 1);
方向 = "z";
}
Debug.WriteLine("面位置为:" + 位置值);
return ((Face)item, 方向, 位置值, eedge);
}).ToList();
Feature firstfeature = (Feature)partDoc.FirstFeature();
while (firstfeature != null)
{
List<Face> 平面集 = new List<Face>();
if (firstfeature.GetTypeName2() == "EdgeFlange")
{
EdgeFlangeFeatureData swEdgeFlange = default(EdgeFlangeFeatureData);
swEdgeFlange = (EdgeFlangeFeatureData)firstfeature.GetDefinition();
double 折弯半径 = swEdgeFlange.BendRadius * 1000;
bool 激活成功 = swDrawing.ActivateView(viewname);
string 特征名称 = firstfeature.Name + "@" + partname + "@" + viewname;
激活成功 = swModel.Extension.SelectByID2(特征名称, "BODYFEATURE", 0, 0, 0, false, 0, null, 0);
Console.WriteLine("特征名称=" + 特征名称 + ",激活成功?" + 激活成功);
var Annotations = (object[])swDrawing.InsertModelAnnotations3(1, 32776, true, false, false, true);
if (Annotations != null)
{
foreach (var Annotation2 in Annotations)
{
Annotation Annotation = (Annotation)Annotation2;
var swDisplayDimension = (DisplayDimension)Annotation.GetSpecificAnnotation();
if (swDisplayDimension.Type2 == (int)swDimensionType_e.swAngularDimension)
{ Annotation.Select(true); swModel.EditDelete(); }
swModel.ClearSelection2(true);
}
}
object[] faces2 = (object[])firstfeature.GetFaces();
var facelist = faces2.Select(item => (Face)item).OrderByDescending(item => item.GetArea()).ToList();
if (facelist.Skip(1).FirstOrDefault() != default)
{
平面集.Add(facelist.FirstOrDefault());
平面集.Add(facelist.Skip(1).FirstOrDefault());
}
foreach (var face in 平面集)
{
Surface plansurface = (Surface)face.GetSurface();
double[] Params = (double[])plansurface.PlaneParams;
(double, double, double) 平面朝向 = (Math.Round(Params[0]), Math.Round(Params[1]), Math.Round(Params[2]));
object[] Edges = (object[])face.GetEdges();
foreach (var edge2 in Edges)
{
Edge edge = (Edge)edge2;
object[] twofaces = (object[])edge.GetTwoAdjacentFaces();
Curve linecurve = (Curve)edge.GetCurve();
double[] lineParams = (double[])linecurve.LineParams;
foreach (var face2 in twofaces)
{
Face oneface = (Face)face2;
Surface surface = (Surface)oneface.GetSurface();
if (surface.IsCylinder())
{
double[] CylinderParams = (double[])surface.CylinderParams;
if (Math.Round(CylinderParams[6] * 1000 - 折弯半径) != 0)
{
bool 计过了 = false;
object[] edges = (object[])oneface.GetEdges();
foreach (var eedge2 in edges)
{
Edge eedge = (Edge)eedge2;
Curve ccurve = (Curve)eedge.GetCurve();
if (ccurve.IsCircle() && !计过了)
{
计过了 = true;
bool 正方向 = false;
string 方向 = "";
double[] CircleParams = (double[])ccurve.CircleParams;
if (平面朝向.Item1 != 0)
{
正方向 = lineParams[0] < CircleParams[0];
方向 = "x";
faces.Add((face, 方向, 正方向, Math.Round(lineParams[0] * 1000, 1)));
}
else if (平面朝向.Item2 != 0)
{
正方向 = lineParams[1] < CircleParams[1];
方向 = "y";
faces.Add((face, 方向, 正方向, Math.Round(lineParams[1] * 1000, 1)));
}
else if (平面朝向.Item3 != 0)
{
正方向 = lineParams[2] < CircleParams[2];
方向 = "z";
faces.Add((face, 方向, 正方向, Math.Round(lineParams[2] * 1000, 1)));
}
Console.WriteLine("面积=" + face.GetArea() * 1000000 + $" ,正方向? {正方向}");
}
}
}
}
}
}
}
}
firstfeature = (Feature)firstfeature.GetNextFeature();
}
Console.WriteLine("faces.count=" + faces.Count);
Dictionary<Face, Face> 标过的面 = new Dictionary<Face, Face>();
foreach (var face实例 in faces)
{
string 放置方式 = "无";
if (face实例.Item2 == "x")
{
if (ViewTransformDATA[0] != 0) { 放置方式 = "水平"; }
else if (ViewTransformDATA[1] != 0) { 放置方式 = "竖直"; }
}
else if (face实例.Item2 == "y")
{
if (ViewTransformDATA[3] != 0) { 放置方式 = "水平"; }
else if (ViewTransformDATA[4] != 0) { 放置方式 = "竖直"; }
}
else if (face实例.Item2 == "z")
{
if (ViewTransformDATA[6] != 0) { 放置方式 = "水平"; }
else if (ViewTransformDATA[7] != 0) { 放置方式 = "竖直"; }
}
Debug.WriteLine("放置方式为:" + 放置方式);
if (放置方式 == "无") continue;
List<Face> 同向量集 = 面集.Where(item => item.Item2 == face实例.Item2).OrderByDescending(item => item.Item3)
.Select(item =>
{
return item.Item1;
}).ToList();
Face 匹配面 = face实例.Item3 ? 同向量集.FirstOrDefault() : 同向量集.LastOrDefault();
Face 标过此面;
if (标过的面.TryGetValue(匹配面, out 标过此面) && 标过此面 == face实例.Item1)
{
Debug.WriteLine("标过此面"); continue;
}
else
{
标过的面.Add(face实例.Item1, 匹配面);
Debug.WriteLine("没标过此面");
}
object[] edges = (object[])匹配面.GetEdges();
object[] edges2 = (object[])face实例.Item1.GetEdges();
double 相差 = 0; int i = 0;
while (true)
{
Vertex vertex = (Vertex)((Edge)edges2[i]).GetStartVertex();
Vertex endvertex = (Vertex)((Edge)edges2[i]).GetEndVertex();
SelectionMgr swSelMgr = (SelectionMgr)swModel.SelectionManager;
SelectData swSelData = swSelMgr.CreateSelectData();
//((Entity)vertex).Select4(false, swSelData);
//((Entity)face实例.Item5).Select4(true, swSelData);
var vPt = (double[])vertex.GetPoint();
var swMathPt = (MathPoint)swMathUtils.CreatePoint(vPt);
swMathPt = (MathPoint)swMathPt.MultiplyTransform(swViewXform);
var MathPtData = (double[])swMathPt.ArrayData;
var endvPt = (double[])endvertex.GetPoint();
var endswMathPt = (MathPoint)swMathUtils.CreatePoint(endvPt);
endswMathPt = (MathPoint)endswMathPt.MultiplyTransform(swViewXform);
var endMathPtData = (double[])endswMathPt.ArrayData;
Console.WriteLine($"swMathPt1:{Math.Round((MathPtData[0] + endMathPtData[0]) / 2, 3)},{Math.Round((MathPtData[1] + endMathPtData[1]) / 2, 3)},{Math.Round((MathPtData[2] + endMathPtData[2]) / 2, 3)}");
if (放置方式 == "水平")
{
相差 = Math.Round(MathPtData[1] * 1000 - endMathPtData[1] * 1000, 1);
Console.WriteLine("相差=" + 相差 + "," + Math.Round(MathPtData[1] * 1000, 3) + "," + Math.Round(endMathPtData[1] * 1000, 3));
}
else if (放置方式 == "竖直")
{
相差 = Math.Round(MathPtData[0] * 1000 - endMathPtData[0] * 1000, 1);
Console.WriteLine("相差=" + 相差 + "," + Math.Round(MathPtData[0] * 1000, 3) + "," + Math.Round(MathPtData[0] * 1000, 3));
}
else
{
Console.WriteLine("放置方式为其他");
}
if (相差 != 0) { swModel.Extension.SelectByRay((MathPtData[0] + endMathPtData[0]) / 2, (MathPtData[1] + endMathPtData[1]) / 2, 0, 0, 0, -1, 0.0001, 1, false, 0, 0); break; }
i++;
}
相差 = 0; i = 0;
while (true)
{
Vertex vertex = (Vertex)((Edge)edges[i]).GetStartVertex();
Vertex endvertex = (Vertex)((Edge)edges[i]).GetEndVertex();
SelectionMgr swSelMgr = (SelectionMgr)swModel.SelectionManager;
SelectData swSelData = swSelMgr.CreateSelectData();
//((Entity)vertex).Select4(false, swSelData);
//((Entity)face实例.Item5).Select4(true, swSelData);
var vPt = (double[])vertex.GetPoint();
var swMathPt = (MathPoint)swMathUtils.CreatePoint(vPt);
swMathPt = (MathPoint)swMathPt.MultiplyTransform(swViewXform);
var MathPtData = (double[])swMathPt.ArrayData;
var endvPt = (double[])endvertex.GetPoint();
var endswMathPt = (MathPoint)swMathUtils.CreatePoint(endvPt);
endswMathPt = (MathPoint)endswMathPt.MultiplyTransform(swViewXform);
var endMathPtData = (double[])endswMathPt.ArrayData;
Console.WriteLine($"swMathPt1:{Math.Round((MathPtData[0] + endMathPtData[0]) / 2, 3)},{Math.Round((MathPtData[1] + endMathPtData[1]) / 2, 3)},{Math.Round((MathPtData[2] + endMathPtData[2]) / 2, 3)}");
if (放置方式 == "水平")
{
相差 = Math.Round(MathPtData[1] * 1000 - endMathPtData[1] * 1000, 1);
}
else if (放置方式 == "竖直")
{
相差 = Math.Round(MathPtData[0] * 1000 - endMathPtData[0] * 1000, 1);
}
if (相差 != 0) { swModel.Extension.SelectByRay((MathPtData[0] + endMathPtData[0]) / 2, (MathPtData[1] + endMathPtData[1]) / 2, 0, 0, 0, -1, 0.0001, 1, false, 0, 0); break; }
i++;
}
double Xpos, Ypos;
if (放置方式 == "水平")
{
Xpos = (vBounds[0] + vBounds[2]) / 2;
Ypos = (vBounds[3] - Offset);
var myDisplayDim = swModel.AddDimension2(Xpos, Ypos, 0);
}
else if (放置方式 == "竖直")
{
Ypos = (vBounds[1] + vBounds[3]) / 2;
Xpos = (vBounds[0] + Offset);
var myDisplayDim = swModel.AddDimension2(Xpos, Ypos, 0);
}
Offset = Offset + 0.005;
swModel.ClearSelection2(true);
}
Debug.WriteLine("标过的面个数=" + 标过的面.Count);
}
}
}
}
}