Beispiel #1
0
        /// <summary>
        /// 零件导出dxf
        /// </summary>
        /// <param name="swApp"></param>
        /// <param name="swPart"></param>
        /// <param name="dxfPath"></param>
        public void PartToDxf(SldWorks swApp, ModelDoc2 swModel, string modulePath)
        {
            PartDoc swPart = (PartDoc)swModel;

            if (swPart == null)
            {
                return;
            }
            string swModelName = swModel.GetPathName();
            string swDxfName   = modulePath + @"\" + swModel.GetTitle().Substring(0, swModel.GetTitle().Length - 6) + "dxf";

            double[] dataAlignment = new double[12];
            dataAlignment[0]  = 0.0;
            dataAlignment[1]  = 0.0;
            dataAlignment[2]  = 0.0;
            dataAlignment[3]  = 1.0;
            dataAlignment[4]  = 0.0;
            dataAlignment[5]  = 0.0;
            dataAlignment[6]  = 0.0;
            dataAlignment[7]  = 1.0;
            dataAlignment[8]  = 0.0;
            dataAlignment[9]  = 0.0;
            dataAlignment[10] = 0.0;
            dataAlignment[11] = 1.0;
            //Array[0], Array[1], Array[2] - XYZ coordinates of new origin
            //Array[3], Array[4], Array[5] - coordinates of new x direction vector
            //Array[6], Array[7], Array[8] - coordinates of new y direction vector
            //判断XYAXIS,长边作为X轴,短的作为Y轴,用于限定拉丝方向
            bool status = false;

            if (swModel.Extension.SelectByID2("XYAXIS", "SKETCH", 0, 0, 0, false, 0, null, 0))
            {
                status = true;
            }
            else if (swModel.Extension.SelectByID2("xyaxis", "SKETCH", 0, 0, 0, false, 0, null, 0))
            {
                status = true;
            }
            else if (swModel.Extension.SelectByID2("XY", "SKETCH", 0, 0, 0, false, 0, null, 0))
            {
                status = true;
            }
            else if (swModel.Extension.SelectByID2("xy", "SKETCH", 0, 0, 0, false, 0, null, 0))
            {
                status = true;
            }
            if (status)
            {
                Feature swFeature      = swModel.SelectionManager.GetSelectedObject6(1, -1);
                Sketch  swSketch       = swFeature.GetSpecificFeature2();
                var     swSketchPoints = swSketch.GetSketchPoints2(); //获取草图中的所有点
                                                                      //用这三个点抓取直线,并判断长度,长边作为X轴,画3D草图的时候一次性画出两条线,不能分两次画出,否则会判断错误
                SketchPoint p0 = swSketchPoints[0];                   //最先画的点
                SketchPoint p1 = swSketchPoints[1];                   //作为坐标原点
                SketchPoint p2 = swSketchPoints[2];                   //最后画的点
                dataAlignment[0] = p1.X * 1000;
                dataAlignment[1] = p1.Y * 1000;
                dataAlignment[2] = p1.X * 1000;
                double l1 = Math.Sqrt(Math.Pow(p0.X - p1.X, 2) + Math.Pow(p0.Y - p1.Y, 2) + Math.Pow(p0.Z - p1.Z, 2));
                double l2 = Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2) + Math.Pow(p2.Z - p1.Z, 2));
                if (l1 > l2)
                {
                    dataAlignment[3] = p0.X * 1000 - p1.X * 1000;
                    dataAlignment[4] = p0.Y * 1000 - p1.Y * 1000;
                    dataAlignment[5] = p0.Z * 1000 - p1.Z * 1000;
                    dataAlignment[6] = p2.X * 1000 - p1.X * 1000;
                    dataAlignment[7] = p2.Y * 1000 - p1.Y * 1000;
                    dataAlignment[8] = p2.Z * 1000 - p1.Z * 1000;
                }
                else
                {
                    dataAlignment[3] = p2.X * 1000 - p1.X * 1000;
                    dataAlignment[4] = p2.Y * 1000 - p1.Y * 1000;
                    dataAlignment[5] = p2.Z * 1000 - p1.Z * 1000;
                    dataAlignment[6] = p0.X * 1000 - p1.X * 1000;
                    dataAlignment[7] = p0.Y * 1000 - p1.Y * 1000;
                    dataAlignment[8] = p0.Z * 1000 - p1.Z * 1000;
                }
            }
            object varAlignment = dataAlignment;

            //Export sheet metal to a single drawing file将钣金零件导出单个dxf文件
            //include flat-pattern geometry,倒数第二位数字1代表钣金展开,options = 1;
            try
            {
                swPart.ExportToDWG2(swDxfName, swModelName, (int)swExportToDWG_e.swExportToDWG_ExportSheetMetal, true, varAlignment, false, false, 1, null);
            }
            catch (Exception ex)
            {
                throw new Exception(swModelName + "导图过程发生异常" + ex.Message);
            }
        }