///<summary> method <c>GetDrawingDocBOMTable</c>
        ///从SolidWorks 的Drawing文档获取到其材料明细表,一般一个文档一个,多余不获取
        ///</summary>
        public void GetDrawingDocBOMTable(ModelDoc2 modelDoc, out BomTableAnnotation swBOMTable, out string configName, out string topFileName)
        {
            Feature feature = modelDoc.IFirstFeature();

            swBOMTable  = null;
            configName  = "";
            topFileName = "";
            BomFeature swBomFeat = null;

            while (feature != null)
            {
                Type type = feature.GetType();
                if ("BomFeat" == feature.GetTypeName())
                {
                    //Console.WriteLine("******************************");
                    //Console.WriteLine("Feature Name : " + feature.Name);

                    swBomFeat = (BomFeature)feature.GetSpecificFeature2();
                    break;
                }

                string name = feature.Name;
                feature = feature.IGetNextFeature();
            }
            if (swBomFeat == null || swBomFeat.GetTableAnnotationCount() <= 0)
            {
                return;
            }
            Feature swFeat      = (Feature)swBomFeat.GetFeature();
            string  featureName = swFeat.Name;

            swBOMTable  = swBomFeat.IGetTableAnnotations(1);// default(TableAnnotation);
            topFileName = swBomFeat.GetReferencedModelName();
            int configCount = swBomFeat.GetConfigurationCount(true);

            //
            //object[] vTableArr = (object[])swBomFeat.GetTableAnnotations();
            //foreach (object vTable_loopVariable in vTableArr)
            //{
            //    object vTable = vTable_loopVariable;
            //    TableAnnotation swTable = (TableAnnotation)vTable;
            //    ProcessTableAnn(modelDoc, swTable);
            //}

            configName = "";
            if (configCount > 0)
            {
                bool visibility = true;
                configName = swBomFeat.IGetConfigurations(true, 0, ref visibility);
            }
            //swBOMTable.Iget
            return;
        }
Exemple #2
0
        /// <summary>
        /// 出工程图
        /// </summary>
        /// <param name="dicValues"></param>
        /// <param name="prtTemp"></param>
        /// <param name="compTemp"></param>
        /// <param name="bomPaths"></param>
        /// <param name="savePaths">保存路径</param>
        /// <param name="newPath">新路径</param>
        private void Drawing(List <string> dicValues, string prtTemp, string compTemp, string bomPaths, string savePaths, string newPath)
        {
            string          viewType   = string.Empty;
            bool            isometric  = false;
            bool            bom        = false;
            string          dimesion   = string.Empty;
            double          spaceX     = 0.06;                  //x方向留白区域
            double          scale      = 1;                     //定义比例初始值
            List <double[]> viewBox    = new List <double[]>(); //集合存放视图box
            List <double[]> viewOrigin = new List <double[]>(); //集合存放视图原点值

            double[] outLine = new double[4];                   //视图box值
            double[] postion = new double[2];                   //视图原点值
            _swApp = (SldWorks)ConnectSW.iSwApp;                //连接sw
            if (_swApp == null)
            {
                return;
            }
            foreach (string item in dicValues)
            {
                int    errors   = 0;
                int    warnings = 0;
                double view1X   = 0;
                double view1Y   = 0;
                string filePath = item;                                             //文件路径
                string fileEx   = filePath.Substring(filePath.LastIndexOf("."), 7); //获得后缀名
                string filename = filePath.Substring(filePath.LastIndexOf("\\") + 1, filePath.LastIndexOf(".") - filePath.LastIndexOf("\\") - 1);
                if (fileEx == ".SLDPRT")
                {
                    _swModelDoc = (ModelDoc2)_swApp.OpenDoc6(filePath, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
                    __swDrawDoc = (DrawingDoc)_swApp.NewDrawing2((int)swDwgTemplates_e.swDwgTemplateCustom, prtTemp, (int)swDwgPaperSizes_e.swDwgPapersUserDefined, 0, 0);
                }
                if (fileEx == ".SLDASM")
                {
                    _swModelDoc = (ModelDoc2)_swApp.OpenDoc6(filePath, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
                    __swDrawDoc = (DrawingDoc)_swApp.NewDrawing2((int)swDwgTemplates_e.swDwgTemplateCustom, compTemp, (int)swDwgPaperSizes_e.swDwgPapersUserDefined, 0, 0);
                }
                _swModelEx = (ModelDocExtension)_swModelDoc.Extension;
                if (newPath != null)
                {
                    savePaths = newPath.Substring(0, newPath.LastIndexOf("\\"));
                }
                //新建工程图
                _swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swAutomaticScaling3ViewDrawings, true); //自动缩放新工程图比例
                if (standarView == false && isometric)                                                               //如果只生成等轴测视图
                {
                    CreatIsometric(__swDrawDoc, filePath);
                    _currentSheet = (Sheet)__swDrawDoc.GetCurrentSheet();
                    __swDrawDoc.ActivateSheet(_currentSheet.GetName());
                    _swView = __swDrawDoc.GetFirstView();
                    _swView = _swView.GetNextView();
                    ChangeViewShow(tag, _swView);
                    _swApp.CloseDoc(_swModelDoc.GetTitle());//
                    ModelDoc2 tempDoc = (ModelDoc2)__swDrawDoc;
                    bool      ss0     = false;
                    if (newPath != null && IfDwg)
                    {
                        ss0 = tempDoc.Extension.SaveAs(newPath, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);
                    }
                    else if (newPath != null && IfDwg == false)
                    {
                        ss0 = tempDoc.Extension.SaveAs(savePaths + "\\" + (_swModelDoc.GetTitle()).Substring(0, (_swModelDoc.GetTitle()).LastIndexOf(".")) + ".DWG", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);
                    }
                    else if (newPath == null && IfDwg)
                    {
                        ss0 = tempDoc.Extension.SaveAs(savePaths + "\\" + (_swModelDoc.GetTitle()).Substring(0, (_swModelDoc.GetTitle()).LastIndexOf(".")) + ".DWG", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);
                    }
                    else
                    {
                        ss0 = tempDoc.Extension.SaveAs(savePaths + "\\" + (_swModelDoc.GetTitle()).Substring(0, (_swModelDoc.GetTitle()).LastIndexOf(".")) + ".slddrw", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);
                    }

                    bool isHidden = tempDoc.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swViewDisplayHideAllTypes, true); //隐藏所有类型
                    _swApp.CloseDoc(tempDoc.GetTitle());
                    _numSucess += 1;                                                                                                //转换成功的数量
                    sucessFile.Add(filePath);
                    return;
                }
                bool isCreate = false;
                #region 第三视角投影
                if (thirProject)
                {
                    isCreate = __swDrawDoc.Create3rdAngleViews2(filePath);//创建第三视角投影视图(视图位置有差别)
                    //调整视图位置
                    _swView = __swDrawDoc.GetFirstView();
                    while (_swView != null)
                    {
                        double[] vBox   = new double[] { };
                        double[] vPoint = new double[] { };
                        vBox   = (double[])_swView.GetOutline();
                        vPoint = (double[])_swView.Position;
                        viewBox.Add(vBox);
                        viewOrigin.Add(vPoint);
                        _swView = _swView.GetNextView();
                    }
                    //调整视图1、2位置
                    if (viewBox[0][3] > viewBox[0][2])//纵向
                    {
                        double[] posView1 = new double[] { viewBox[0][2] / 3.0, viewOrigin[1][1] };
                        _swView          = __swDrawDoc.GetFirstView();
                        _swView          = _swView.GetNextView();
                        _swView.Position = posView1;
                        double[] posView2 = new double[] { viewOrigin[2][0], viewOrigin[2][1] - (viewBox[2][1] - viewBox[1][3]) / 1.9 };//调整视图2位置
                        _swView          = _swView.GetNextView();
                        _swView.Position = posView2;
                    }
                    else
                    {
                        double[] posView1 = new double[] { viewBox[0][2] / 4.0, viewOrigin[1][1] };
                        _swView          = __swDrawDoc.GetFirstView();
                        _swView          = _swView.GetNextView();
                        _swView.Position = posView1;
                        double[] posView2 = new double[] { viewOrigin[2][0], viewOrigin[2][1] - (viewBox[2][1] - viewBox[1][3]) / 2.0 };//调整视图2位置
                        _swView          = _swView.GetNextView();
                        _swView.Position = posView2;
                    }
                    viewOrigin.Clear();
                    viewBox.Clear();
                    _swView = __swDrawDoc.GetFirstView();
                    while (_swView != null)
                    {
                        double[] vBox   = new double[] { };
                        double[] vPoint = new double[] { };
                        vBox   = (double[])_swView.GetOutline();
                        vPoint = (double[])_swView.Position;
                        viewBox.Add(vBox);
                        viewOrigin.Add(vPoint);
                        _swView = _swView.GetNextView();
                    }
                    if (isometric)//等轴测
                    {
                        _swView = (IView)__swDrawDoc.CreateDrawViewFromModelView3(filePath, "*等轴测", viewOrigin[3][0], viewOrigin[2][1], 0);
                        _swView = __swDrawDoc.GetFirstView();
                        double[] sca = new double[2];
                        sca                = _swView.ScaleRatio;//获取比例
                        _swView            = _swView.GetNextView();
                        _swView            = _swView.GetNextView();
                        _swView            = _swView.GetNextView();
                        _swView            = _swView.GetNextView();
                        _swView.ScaleRatio = sca;
                        double[] rePos = new double[] { viewOrigin[3][0] + 0.015, viewOrigin[2][1] };
                        _swView.Position = rePos;
                        ChangeViewShow(tag, _swView);                     //视图样式
                        AddDimension(dimesion, __swDrawDoc, _swModelDoc); //尺寸标注
                    }
                }
                #endregion
                #region 第一视角投影
                else
                {
                    isCreate = __swDrawDoc.Create1stAngleViews(filePath);//创建第一视角投影视图
                    //获取视图boundingbox
                    _swView = __swDrawDoc.GetFirstView();
                    while ((_swView != null))
                    {
                        outLine = (double[])_swView.GetOutline(); //(x,y坐标最值)
                        postion = (double[])_swView.Position;     //视图位置值
                        viewBox.Add(outLine);
                        viewOrigin.Add(postion);
                        _swView = _swView.GetNextView();
                    }

                    double newScale = CalcuScale(viewBox, __swDrawDoc, scale, spaceX, 3);
                    //调整视图位置(position)
                    if (viewBox[0][3] > viewBox[0][2])
                    {
                        view1X = viewBox[0][2] / 2.8;                  //视图1的X坐标放在图纸坐标的三分之一处
                        view1Y = viewBox[0][3] - viewBox[0][3] / 4.85; //视图1的Y坐标放在图纸坐标的五分之一处
                    }
                    else
                    {
                        view1X = viewBox[0][2] / 3.5;                 //视图1的X坐标放在图纸坐标的三分之一处
                        view1Y = viewBox[0][3] - viewBox[0][3] / 4.0; //视图1的Y坐标放在图纸坐标的四分之一处
                    }
                    double[] view1Pos = new double[] { view1X, view1Y };
                    _swView          = __swDrawDoc.GetFirstView(); //获取图纸
                    _swView          = _swView.GetNextView();      //获取第一个视图
                    _swView.Position = view1Pos;
                    bool isRebuild = _swModelDoc.EditRebuild3();
                    //viewBox.Clear();
                    List <double[]> tempBox = new List <double[]>();
                    viewOrigin.Clear();
                    while ((_swView != null))
                    {
                        ChangeViewShow(tag, _swView);                     //改变视图显示样式
                        AddDimension(dimesion, __swDrawDoc, _swModelDoc); //添加尺寸
                        outLine = (double[])_swView.GetOutline();         //(x,y坐标最值)
                        postion = (double[])_swView.Position;             //视图位置值
                        tempBox.Add(outLine);
                        viewOrigin.Add(postion);
                        _swView = _swView.GetNextView();
                    }
                    if (isometric)  //是否创建等轴测视图(非钣金)
                    {
                        double[] scalArry = new double[2];
                        _swView = (IView)__swDrawDoc.CreateDrawViewFromModelView3(filePath, "*等轴测", viewOrigin[2][0], viewOrigin[1][1], 0);
                        _swView = __swDrawDoc.GetFirstView(); //获取图纸
                        _swView = _swView.GetNextView();      //获取第一个视图
                        double sca = _swView.get_IScaleRatio();
                        if (sca == 1)                         //缩小比例
                        {
                            scalArry[0] = 1;
                            scalArry[1] = newScale;
                        }
                        else
                        {
                            scalArry[0] = sca;
                            scalArry[1] = 1;
                        }
                        _swView            = _swView.GetNextView();
                        _swView            = _swView.GetNextView();
                        _swView            = _swView.GetNextView();
                        _swView.ScaleRatio = scalArry;//调整轴测图比例
                        ChangeViewShow(tag, _swView);
                        _swModelDoc.EditRebuild3();
                    }
                    ////关掉文件(不保存)
                    //_swApp.CloseDoc(_swModelDoc.GetTitle());
                    if (fileEx == ".SLDASM" && bom)        //是否创建明细表
                    {
                        //viewBox.Clear();
                        //viewOrigin.Clear();
                        double[] outArry  = new double[4];
                        double[] posArry  = new double[2];
                        double[] posArry1 = null;
                        double[] posArry2 = null;
                        double[] posArry3 = null;
                        _currentSheet = (Sheet)__swDrawDoc.GetCurrentSheet();
                        __swDrawDoc.ActivateSheet(_currentSheet.GetName());
                        _swView        = __swDrawDoc.GetFirstView(); //获取图纸
                        _swView        = _swView.GetNextView();      //获取第一个视图
                        swBomTableAnno = (BomTableAnnotation)_swView.InsertBomTable4(true, 0.4, 0.1, (int)swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_BottomRight, (int)swBomType_e.swBomType_TopLevelOnly, "", bomPaths, false, 0, false);
                        TableAnnotation tableAnnotation = (TableAnnotation)swBomTableAnno;
                        tableAnnotation.GetAnnotation();
                        BomFeature bomFeature     = swBomTableAnno.BomFeature;
                        bool[]     array          = new bool[bomFeature.GetConfigurationCount(false)];
                        object     Visible        = array;
                        object     configurations = bomFeature.GetConfigurations(false, ref Visible);
                        array[0] = true;
                        bomFeature.SetConfigurations(true, array, configurations);
                        FeatureManager featureManager = _swModelDoc.FeatureManager;
                        featureManager.UpdateFeatureTree();
                        double num = 0.007;
                        tableAnnotation.SetRowHeight(-2, num, 0);
                        double totleHeight = num * tableAnnotation.RowCount; //计算总的tableBom高度
                        double heightSum   = totleHeight + 0.070;            //加上标题栏总高度
                        int    columCount  = tableAnnotation.ColumnCount;    //获取总的列数
                        double columWidth  = 0;
                        for (int i = 0; i < columCount; i++)
                        {
                            double wid = tableAnnotation.GetColumnWidth(i);
                            columWidth += wid;
                        }
                        _swView = _swView.GetNextView();
                        _swView = _swView.GetNextView();
                        outArry = (double[])_swView.GetOutline();
                        if (outArry[1] < heightSum)//有可能被遮挡
                        {
                            //__swView = _swDrawDoc.GetFirstView();
                            //double[] tempArry = new double[] { };
                            //tempArry=(double[])__swView.GetOutline();
                            //viewBox.Insert(0, tempArry);
                            scale = ((viewBox[1][2] - viewBox[1][0]) + (viewBox[3][2] - viewBox[3][0])) / (viewBox[0][2] - columWidth - 0.025);//计算X方向的比例
                            if (scale > 11)
                            {
                                double scaleCeil = System.Math.Ceiling(scale);
                                if (scaleCeil > 11 && scaleCeil < 16)
                                {
                                    scale = 20;
                                }
                                if (scaleCeil > 15 && scaleCeil < 21)
                                {
                                    scale = 25;
                                }
                                if (scaleCeil > 20 && scaleCeil < 26)
                                {
                                    scale = 30;
                                }
                                if (scaleCeil > 25 && scaleCeil < 34)
                                {
                                    scale = 40;
                                }
                                if (scaleCeil > 33 && scaleCeil < 45)
                                {
                                    scale = 50;
                                }
                                if (scaleCeil > 44 && scaleCeil < 55)
                                {
                                    scale = 60;
                                }
                            }
                            else if (scale > 5.8 && scale < 10.5)
                            {
                                double scaleCeil = System.Math.Ceiling(scale);
                                //scale = scaleCeil + 4.0;          //比例为10、11、12、13、14、15
                                if (scaleCeil > 5 && scaleCeil < 8)
                                {
                                    scale = 10;
                                }
                                if (scaleCeil > 7 && scaleCeil < 12)
                                {
                                    scale = 15;
                                }
                            }
                            _currentSheet = (Sheet)__swDrawDoc.GetCurrentSheet();
                            __swDrawDoc.ActivateSheet(_currentSheet.GetName());
                            bool isScale = _currentSheet.SetScale(1, scale, false, false); //设置视图和图纸比例
                            _swView = __swDrawDoc.GetFirstView();
                            _swView = _swView.GetNextView();                               //获取视图1
                            double[] pos1 = new double[2];
                            pos1             = _swView.Position;
                            posArry1         = new double[] { pos1[0] - columWidth / 3.45, pos1[1] };
                            _swView.Position = posArry1;              //重置视图1的位置
                            _swView          = _swView.GetNextView(); //获取视图2
                            double[] pos2 = new double[2];
                            pos2             = _swView.Position;
                            posArry2         = new double[] { pos2[0] - columWidth / 3.45, pos2[1] + 0.0015 };
                            _swView.Position = posArry2;              //设置视图2位置
                            _swView          = _swView.GetNextView(); //获取视图三
                            posArry          = (double[])_swView.Position;
                            posArry3         = new double[] { posArry[0] - columWidth / 3.5, posArry[1] };
                            _swView.Position = posArry3;//视图3坐标
                        }
                    }
                }//第一视角投影
                #endregion
                if (isCreate)
                {
                    _numSucess += 1;//转换成功的数量
                    sucessFile.Add(filePath);
                }
                ModelDoc2 tempModelDoc = (ModelDoc2)__swDrawDoc;                                                                    //获取到swDraw的父级(ModelDoc)
                _swApp.CloseDoc(_swModelDoc.GetTitle());
                bool isHiden = tempModelDoc.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swViewDisplayHideAllTypes, true); //隐藏所有类型

                _swModelDoc.ForceRebuild3(false);
                string saveName = (_swModelDoc.GetTitle()).Substring(0, (_swModelDoc.GetTitle()).LastIndexOf("."));
                bool   ss;
                if (newPath != null && IfDwg)
                {
                    ss = tempModelDoc.Extension.SaveAs(savePaths + "\\" + (_swModelDoc.GetTitle()) + ".DWG", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);
                }
                else if (newPath != null && IfDwg == false)
                {
                    ss = tempModelDoc.Extension.SaveAs(savePaths + "\\" + (_swModelDoc.GetTitle()) + ".slddrw", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);
                }
                else if (newPath == null && IfDwg)
                {
                    ss = tempModelDoc.Extension.SaveAs(savePaths + "\\" + (_swModelDoc.GetTitle()) + ".DWG", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);
                }
                else
                {
                    ss = tempModelDoc.Extension.SaveAs(savePaths + "\\" + (_swModelDoc.GetTitle()) + ".slddrw", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);
                }
                _swApp.CloseDoc(tempModelDoc.GetTitle());//关闭转换完成的文件
            }
        }