Exemplo n.º 1
0
        private void btnAssem1_Click(object sender, EventArgs e)
        {
            if (swApp == null)
            {
                swApp = (SldWorks)ConnectSW.iSwApp;
            }
            List <string> fileAssem1    = new List <string>(); //文件路径集合
            string        carrerPath    = string.Empty;        //行星架路径
            string        planetaryPath = string.Empty;        //行星轮路径
            string        outShaftPath  = string.Empty;        //输出轴路径
            string        pineShaftPath = string.Empty;        //销轴路径

            //获取界面中的文件并打开
            int Rows = this.lBoxAssem1.Items.Count;  //行的数量

            if (Rows == 0)
            {
                MessageBox.Show("请选择装配零部件!");
                return;
            }
            foreach (object item in lBoxAssem1.Items)
            {
                fileAssem1.Add(item.ToString());   //界面中的文件
            }
            foreach (string file in fileAssem1)    //遍历文件获取路径
            {
                string fileName = file.Substring(file.LastIndexOf("\\") + 1);
                if (fileName.Contains("架"))
                {
                    carrerPath = file;
                }
                if (fileName.Contains("行星轮"))
                {
                    planetaryPath = file;
                }
                if (fileName.Contains("输出"))
                {
                    outShaftPath = file;
                }
                if (fileName.Contains("销轴"))
                {
                    pineShaftPath = file;
                }
                //swModel = (ModelDoc2)swApp.OpenDoc6(file, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            }

            //实现装配
            //打开装配模板(行星架+输出轴+销轴+行星轮+行星轮垫片+轴承)然后替换相关零部件
            swModel = (ModelDoc2)swApp.OpenDoc6(_exePath + "PrtAndAsmTemp\\subAssemly.sldasm", (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            //遍历装配体选择零部件
            swModel    = (ModelDoc2)swApp.ActiveDoc;
            swConfMgr  = (SolidWorks.Interop.sldworks.ConfigurationManager)swModel.ConfigurationManager;
            swConf     = (SolidWorks.Interop.sldworks.Configuration)swConfMgr.ActiveConfiguration;
            swRootComp = (Component2)swConf.GetRootComponent();
            //TraverseAssembly(swRootComp, 1);
            object[]   vChildComp;
            Component2 swChildComp;

            vChildComp = (object[])swRootComp.GetChildren();
            AssemblyDoc swTempAssem = (AssemblyDoc)swModel;

            for (int i = 0; i < vChildComp.Length; i++)
            {
                swChildComp = (Component2)vChildComp[i];
                string    compName    = swChildComp.Name2;
                ModelDoc2 swModelTemp = swChildComp.GetModelDoc2();    //转换成ModelDoc2
                //选中要替换掉的零件
                SelectData seldate = ((SelectionMgr)swModelTemp.SelectionManager).CreateSelectData();
                swChildComp.Select4(false, seldate, false);
                //替换零部件
                if (compName == "行星架-1")
                {
                    bool isTrue = swTempAssem.ReplaceComponents(carrerPath, "", true, true);  //零件地址
                }
                if (compName == "行星轮-1")
                {
                    swTempAssem.ReplaceComponents(planetaryPath, "", true, true);    //零件地址
                }
                if (compName == "输出轴-1")
                {
                    swTempAssem.ReplaceComponents(outShaftPath, "", true, true);    //零件地址
                }
                if (compName == "销轴-1")
                {
                    swTempAssem.ReplaceComponents(pineShaftPath, "", true, true);    //零件地址
                }
                if (compName == "行星架垫片-1")
                {
                    swTempAssem.ReplaceComponents("", "", true, true);    //零件地址
                }
            }
            swModel.EditRebuild3();
        }
Exemplo n.º 2
0
        private void btnAssembly_Click_1(object sender, EventArgs e)
        {
            if (swApp == null)
            {
                ConnectSw();
            }

            List <string> fileAssem1 = new List <string>();        //文件路径集合
            int           Rows       = this.lBoxFile3.Items.Count; //行的数量

            if (Rows == 0)
            {
                MessageBox.Show("请选择装配零部件!");
                return;
            }
            string FaLan         = string.Empty; //电机法兰路径
            string InnerGearPath = string.Empty; //内齿轮路径
            string standPath     = string.Empty; //机座路径
            string subAssemPath  = string.Empty; //子装配体路径

            foreach (object item in lBoxFile3.Items)
            {
                fileAssem1.Add(item.ToString()); //界面中的文件
            }
            foreach (string file in fileAssem1)  //遍历文件获取路径
            {
                string fileName = file.Substring(file.LastIndexOf("\\") + 1);
                if (fileName.Contains("法兰"))
                {
                    FaLan = file;
                }
                if (fileName.Contains("内齿"))
                {
                    InnerGearPath = file;
                }
                if (fileName.Contains("机座"))
                {
                    standPath = file;
                }
                if (fileName.Contains("子装配"))
                {
                    subAssemPath = file;
                }
            }
            swModel = (ModelDoc2)swApp.OpenDoc6(_exePath + "PrtAndAsmTemp\\总装配体.sldasm", (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            //遍历装配体选择零部件
            swModel    = (ModelDoc2)swApp.ActiveDoc;
            swConfMgr  = (SolidWorks.Interop.sldworks.ConfigurationManager)swModel.ConfigurationManager;
            swConf     = (SolidWorks.Interop.sldworks.Configuration)swConfMgr.ActiveConfiguration;
            swRootComp = (Component2)swConf.GetRootComponent();
            object[]   vChildComp;
            Component2 swChildComp;

            vChildComp = (object[])swRootComp.GetChildren();
            AssemblyDoc swTempAssem = (AssemblyDoc)swModel;

            for (int i = 0; i < vChildComp.Length; i++)
            {
                swChildComp = (Component2)vChildComp[i];
                string    compName    = swChildComp.Name2;
                ModelDoc2 swModelTemp = swChildComp.GetModelDoc2();//转换成ModelDoc2
                //选中要替换掉的零件
                SelectData seldate = ((SelectionMgr)swModelTemp.SelectionManager).CreateSelectData();
                swChildComp.Select4(false, seldate, false);
                //替换零部件
                if (compName == "电机法兰-1")
                {
                    bool isTrue = swTempAssem.ReplaceComponents(FaLan, "", true, true);//零件地址
                }
                if (compName == "内齿轮-1")
                {
                    swTempAssem.ReplaceComponents(InnerGearPath, "", true, true);//零件地址
                }
                if (compName == "立机座-1")
                {
                    swTempAssem.ReplaceComponents(standPath, "", true, true);//零件地址
                }
                if (compName == "子装配2-1")
                {
                    swTempAssem.ReplaceComponents(subAssemPath, "", true, true);//轴套子装配地址
                }
                if (compName == "subAssemly-1")
                {
                    swTempAssem.ReplaceComponents("", "", true, true);//行星架子装配
                }
            }
            swModel.EditRebuild3();
        }