一、前言
昨天分享了自动出2D零件图的思路(UG/NX 二次开发(C#)自动出2D零件图思路),今天整理了Q群里各位大佬的意见,结合当前实际项目情况,做了一个可配置的半自动出图版本,暂且称之为标准件配置Bata版。
虽然没能实现自动出2D零件图,但是拿着去跟老板交差,应该勉强过关吧,毕竟也年底了,大家都想早点回家过年。
二、实现原理
先引入昨天的思路:
1、出图方案设置
出图方式千千万,每个人都有自己的想法,我们需要先定一个标准,然后程序才能按照标准自动出图,标准如下:
1)图纸参数设置,包括图纸类型,视图比例,图纸名称
2)视图布局方案,根据用户需求对视图进行布局,视图包括:顶视图、左视图、右视图、后视图、底视图、正三轴视图、右剖视图、底剖视图。
3)标注方案,目前我想到的先从简单的平面尺寸开始,先做水平、垂直、角度的标注
2、自动创建图纸、自动创建视图
3、自动标注
经过今天的梳理,再整理一个版本:
1、创建图纸
涉及到图纸参数设置,这个小意思容易解决
2、创建视图
需要根据需求配置视图方案,个人认为做成配置文件即可,先放一放
3、创建尺寸标注
重点来了,如何创建标注才是自动出图的重头戏,因为每个视图的情况不一样,不同产品在不同视图里面的情况就更复杂了。所以今天引入了标准件和配置的概念。既然一口吃不了一个胖子,就先把标准的东西搞出来。
此版本效果图如下:
三、详细方案
这里只针对标准件做自动出图,而且加入了出图配置文件,也就是根据配置文件程序一步一步的执行,先看看配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<Draw>
<view title="顶视图" name="TOP" point="120, 200, 0.0">
<dim type="CreateVerticalDim" edgeStart="EDGESTART" edgeEnd="EDGEEND" dimension_3d_origin="50, 210, 0"></dim>
<dim type="CreateParallelDim" edgeStart="EDGE3" edgeEnd="EDGEEND" dimension_3d_origin="57, 165, 0"></dim>
<dim type="CreateParallelDim" edgeStart="EDGE1" edgeEnd="EDGE2" dimension_3d_origin="120, 265, 0"></dim>
<dim></dim>
</view>
<view title="左视图" name="LEFT" point="120, 80, 0.0">
<dim type="CreateDiameterDim" holeFlag="hole" dimension_3d_origin="140, 100, 0.0"></dim>
</view>
<view title="正三轴视图" name="TFR-ISO" point="300, 180, 0.0">
</view>
</Draw>
view标签代表出图的视图,标签详情包括了视图类别、视图的位置坐标
dim标签代表每一个尺寸标注, 标签详情包括了标注的类别、起始位置、坐标
有这些信息加持,自动出尺寸标注就很清晰了吧
下面正式开始进入程序分享:
1、新建图纸
Tag dragTag = DrawTool.CreatDWG(workPart, "Sheet1");
/// <summary>
/// 创建图纸
/// </summary>
/// <param name="workPart"></param>
/// <returns></returns>
public static Tag CreatDWG(Part workPart,string sheetName)
{
NXOpen.Drawings.DrawingSheet nullDrawings_DrawingSheet = null;
NXOpen.Drawings.DrawingSheetBuilder drawingSheetBuilder1;
drawingSheetBuilder1 = workPart.DrawingSheets.DrawingSheetBuilder(nullDrawings_DrawingSheet);
drawingSheetBuilder1.StandardMetricScale = NXOpen.Drawings.DrawingSheetBuilder.SheetStandardMetricScale.S11;
drawingSheetBuilder1.EnglishSheetTemplateLocation = "";
drawingSheetBuilder1.Height = 297.0;
drawingSheetBuilder1.Length = 420.0;
drawingSheetBuilder1.StandardMetricScale = NXOpen.Drawings.DrawingSheetBuilder.SheetStandardMetricScale.S11;
drawingSheetBuilder1.StandardEnglishScale = NXOpen.Drawings.DrawingSheetBuilder.SheetStandardEnglishScale.S11;
drawingSheetBuilder1.ScaleNumerator = 1.0;
drawingSheetBuilder1.ScaleDenominator = 1.0;
drawingSheetBuilder1.Units = NXOpen.Drawings.DrawingSheetBuilder.SheetUnits.Metric;
drawingSheetBuilder1.ProjectionAngle = NXOpen.Drawings.DrawingSheetBuilder.SheetProjectionAngle.First;
drawingSheetBuilder1.Number = "1";
drawingSheetBuilder1.SecondaryNumber = "";
drawingSheetBuilder1.Revision = "A";
drawingSheetBuilder1.Name = sheetName;
drawingSheetBuilder1.MetricSheetTemplateLocation = "D:\\Program Files\\Siemens\\NX 8.0\\localization\\prc\\simpl_chinese\\startup\\A3-noviews-template.prt";
NXObject nXObject1;
nXObject1 = drawingSheetBuilder1.Commit();
drawingSheetBuilder1.Destroy();
return nXObject1.Tag;
}
2、读取配置文件,并遍历view,创建视图
//读取配置文件
XmlDocument xml = new XmlDocument();
xml.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + @"dimconfig.xml");
XmlNode xn = xml.SelectSingleNode("Draw");
XmlNodeList xnl = xn.ChildNodes;
Tag topViewTag, leftViewTag, tfrViewTag;
Tag topDrawViewTag, leftDrawViewTag, tfrDrawViewTag;
Tag dimTag1, dimTag2, dimTag3;
string strPoint;
Point3d point = new Point3d();
string edgeStart, edgeEnd, holeFlag, dimension_3d_origin_str;
double[] dimension_3d_origin;
Tag edgeStartTag = Tag.Null;
Tag edgeEndTag = Tag.Null;
Tag holeTag = Tag.Null;
//创建视图
foreach (XmlNode xnitem in xnl)
{
XmlElement xe = (XmlElement)xnitem;
string name = xe.GetAttribute("name");
strPoint = xe.GetAttribute("point");
point = GetPointFromStr(strPoint);
if (name == "TOP")
{
DrawTool.CreateView(dragTag, name, point, out topViewTag, out topDrawViewTag);
}
else if(name == "LEFT")
{
DrawTool.CreateView(dragTag, name, point, out leftViewTag, out leftDrawViewTag);
}
else if (name == "TFR-ISO")
{
DrawTool.CreateView(dragTag, name, point, out tfrViewTag, out tfrDrawViewTag);
}
else
{
}
}
3、创建视图的同时,遍历view标签下的dim标签,创建尺寸标注
//创建尺寸标注
XmlNodeList cldList = xnitem.ChildNodes;
foreach (XmlNode item in cldList)
{
XmlElement xeitem = (XmlElement)item;
string type = xeitem.GetAttribute("type");
edgeStart = xeitem.GetAttribute("edgeStart");
edgeEnd = xeitem.GetAttribute("edgeEnd");
holeFlag = xeitem.GetAttribute("holeFlag");
dimension_3d_origin_str = xeitem.GetAttribute("dimension_3d_origin");
dimension_3d_origin = GetOriginFromStr(dimension_3d_origin_str);
edgeStartTag = Tag.Null;
edgeEndTag = Tag.Null;
holeTag = Tag.Null;
theUFSession.Obj.CycleByName(edgeStart, ref edgeStartTag);
theUFSession.Obj.CycleByName(edgeEnd, ref edgeEndTag);
theUFSession.Obj.CycleByName(holeFlag, ref holeTag);
if (type == "CreateVerticalDim")
{
dimTag1 = Tag.Null;
DrawTool.CreateVerticalDim(leftDrawViewTag, edgeStartTag, edgeEndTag, dimension_3d_origin, out dimTag1);
}
else if (type == "CreateParallelDim")
{
dimTag2 = Tag.Null;
DrawTool.CreateParallelDim(leftDrawViewTag, edgeStartTag, edgeEndTag, dimension_3d_origin, out dimTag2);
}
else if (type == "CreateDiameterDim")
{
dimTag3 = Tag.Null;
DrawTool.CreateDiameterDim(leftDrawViewTag, holeTag, dimension_3d_origin, out dimTag3);
}
}
四、总结
这个版本已初具自动出2D图的雏形,能满足标准件经过配置后出图的基本需求,后面会继续挖掘并分享给大家,如果对源码感兴趣可以给我留言索要。
同时也欢迎各位大佬提供宝贵意见和建议。