UG/NX 二次开发(C#)自动出2D零件图思路

一、前言

项目需要自动出2D零件图,可是我之前没做过这方面的内容,没有一点思路。然后我就做了下面几件事:

1、百度、google翻了一遍,搜索关键字“UG二次开发自动出图”

2、csdn.com 和 cnblogs.com翻了一遍

3、平时逛的qq技术群,微信技术群问候了一遍

4、做二开之后结交的技术大牛、小达人请教了一遍

现在有了一点思路,整理出来,自己也写了一部分主要源码分享出来供大家欣赏。

二、思路整理

自动出图方案经过我一番辛苦整理后,需要完成以下几步:

1、出图方案设置

     出图方式千千万,每个人都有自己的想法,我们需要先定一个标准,然后程序才能按照标准自动出图,标准如下:

      1)图纸参数设置,包括图纸类型,视图比例,图纸名称

      2)视图布局方案,根据用户需求对视图进行布局,视图包括:顶视图、左视图、右视图、后视图、底视图、正三轴视图、右剖视图、底剖视图。

      3)标注方案,目前我想到的先从简单的平面尺寸开始,先做水平、垂直、角度的标注

2、自动创建图纸、自动创建视图

3、自动标注

三、已完成部分源码分享

1、创建图纸

/// 
/// 创建图纸
/// 
/// 
/// 
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、创建视图

/// 
/// 创建基本视图(ufun)
/// 
/// 
public static void CreateView(Tag drawTag, string viewName, Point3d point,out Tag viewTag, out Tag drawViewTag)
{
    Session theSession = Session.GetSession();
    Part workPart = theSession.Parts.Work;
    Part displayPart = theSession.Parts.Display;
    theUFSession = UFSession.GetUFSession();
    viewTag = Tag.Null;
    //根据名字获取视图的Tag
    theUFSession.Obj.CycleByNameAndType(workPart.Tag, viewName, UFConstants.UF_view_type, false, ref viewTag);
    double[] dwg_point = new double[2] { point.X, point.Y };
    UFDraw.ViewInfo viewInfo = new UFDraw.ViewInfo();
    theUFSession.Draw.InitializeViewInfo(out viewInfo);
    drawViewTag = Tag.Null;
    //初始化视图信息
    theUFSession.Draw.ImportView(drawTag, viewTag, dwg_point, ref viewInfo, out drawViewTag);
    theUFSession.Draw.UpdateOneView(drawTag, drawViewTag);//更新视图
}

 这里通过ufun方法创建视图,可以完成根据viewName创建各种视图,包括顶视图、左视图、右视图、后视图、底视图、正三轴视图、右剖视图、底剖视图。

目前这个方法能基本符合需求,难点在于视图的位置point怎么确定下来? 

3、创建垂直标注尺寸 

//ufun创建垂直尺寸
UFDrf.Object object1 = new UFDrf.Object();
UFDrf.Object object2 = new UFDrf.Object();
object1.object_view_tag = topDrawViewTag;
object1.object_assoc_type = UFDrf.AssocType.EndPoint;
object1.object_assoc_modifier = UFConstants.UF_DRF_last_end_point;
object1.object_tag = edgeStart;
object2.object_view_tag = topDrawViewTag;
object2.object_assoc_type = UFDrf.AssocType.EndPoint;
object2.object_assoc_modifier = UFConstants.UF_DRF_first_end_point;
object2.object_tag = edgeEnd;

UFDrf.Text drf_text = new UFDrf.Text();
drf_text.user_dim_text = "";
drf_text.lines_app_text = 0;
drf_text.appended_text = "";
double[] dimension_3d_origin = new double[3] { 50, 200, 0 };
Tag dimTag1 = Tag.Null;
theUFSession.Drf.CreateVerticalDim(ref object1, ref object2, ref drf_text, dimension_3d_origin, out dimTag1);

使用了ufun函数theUFSession.Drf.CreateVerticalDim来创建垂直尺寸,能实现标注的创建,但是问题还有很多:

1、各种视图分别需要哪些标注

2、如何创建需要的标注

3、标准的摆放位置的确定也是一个难点 

4、调用主方法

public static int Main(string[] args)
{
    theSession = Session.GetSession();
    theUFSession = UFSession.GetUFSession();
    displayPart = theSession.Parts.Display;
    workPart = theSession.Parts.Work;
    theUI = UI.GetUI();

    int retValue = 0;
    try
    {
        #region 创建标注

        Body rightBody = null;
        BodyTool.GetRightBody(theSession, out rightBody);
        Auto_Init.BodyInit init = new Auto_Init.BodyInit(rightBody);
        BodyModel model = init.ProcBody();

        int moduleid;
        theUFSession.UF.AskApplicationModule(out moduleid);
        if (moduleid != UFConstants.UF_APP_DRAFTING)
        {
            theUI.MenuBarManager.ApplicationSwitchRequest("UG_APP_DRAFTING");
        }

        //新建图纸
        Tag topViewTag, leftViewTag, tfrViewTag;
        Tag topDrawViewTag, leftDrawViewTag, tfrDrawViewTag;
        Tag dragTag = DrawTool.CreatDWG(workPart, "Sheet1");
        Point3d point1 = new Point3d(120, 200, 0.0);
        DrawTool.CreateView(dragTag, "TOP", point1, out topViewTag, out topDrawViewTag);
        Point3d point2 = new Point3d(120, 80, 0.0);
        DrawTool.CreateView(dragTag, "LEFT", point2, out leftViewTag, out leftDrawViewTag);
        Point3d point3 = new Point3d(300, 180, 0.0);
        DrawTool.CreateView(dragTag, "TFR-ISO", point3, out tfrViewTag, out tfrDrawViewTag);

        Tag edgeStart = Tag.Null;
        Tag edgeEnd = Tag.Null;
        theUFSession.Obj.CycleByName("EDGESTART1", ref edgeStart);
        theUFSession.Obj.CycleByName("EDGEEND1", ref edgeEnd);

        //ufun创建垂直尺寸
        UFDrf.Object object1 = new UFDrf.Object();
        UFDrf.Object object2 = new UFDrf.Object();
        object1.object_view_tag = topDrawViewTag;
        object1.object_assoc_type = UFDrf.AssocType.EndPoint;
        object1.object_assoc_modifier = UFConstants.UF_DRF_last_end_point;
        object1.object_tag = edgeStart;
        object2.object_view_tag = topDrawViewTag;
        object2.object_assoc_type = UFDrf.AssocType.EndPoint;
        object2.object_assoc_modifier = UFConstants.UF_DRF_first_end_point;
        object2.object_tag = edgeEnd;

        UFDrf.Text drf_text = new UFDrf.Text();
        drf_text.user_dim_text = "";
        drf_text.lines_app_text = 0;
        drf_text.appended_text = "";
        double[] dimension_3d_origin = new double[3] { 50, 200, 0 };
        Tag dimTag1 = Tag.Null;
        theUFSession.Drf.CreateVerticalDim(ref object1, ref object2, ref drf_text, dimension_3d_origin, out dimTag1);

        #endregion           
    }
    catch (NXOpen.NXException ex)
    {

    }
    return retValue;
}

 

四、总结

        以上只是一个不完整的思路和实现方案,希望有懂的大佬给一些建议,后续我会继续完善这一块内容并无偿分享给大家。

附一张当前完成度的效果图:

UG/NX 二次开发(C#)自动出2D零件图思路_第1张图片

 

你可能感兴趣的:(二次开发NXOPEN,c#,开发语言,自动出图)