Пример #1
0
        private void btnGearShaftModelBuilding_Click(object sender, EventArgs e)
        {
            //类变量,调研前要赋值
            // xlmGearShaftModelFileName = @"C:\齿轮轴.xml";//齿轮的XML文件名字

            XElement xe = XElement.Load(strModelFileName);

            //读取齿轮模型文件名字
            IEnumerable<XElement> elements = from 齿轮轴名称 in xe.Elements("基本信息")// where PInfo.Attribute("ID").Value == strID
                                             select 齿轮轴名称;
            string modelFile = elements.ElementAt(0).Element("项目目录").Value + elements.ElementAt(0).Element("齿轮轴名称").Value;
            //利用齿轮模型文件名字,读取齿轮模型
            string partTemplate = SwAddin.iSwApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
            if ((partTemplate != null) && (partTemplate != ""))
            {
                modDoc = (IModelDoc2)SwAddin.iSwApp.OpenDoc6(modelFile,
                    (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "",
                    (int)swFileLoadError_e.swGenericError, (int)swFileLoadWarning_e.swFileLoadWarning_AlreadyOpen);
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("There is no part template available. Please check your options and make sure there is a part template selected, or select a new part template.");
            }

            elements = from 基圆直径 in xe.Elements("尺寸计算")// where PInfo.Attribute("ID").Value == strID
                       select 基圆直径;
            double d_b = double.Parse(elements.ElementAt(0).Element("基圆直径").Value) / 1000;          //基圆直径
            double d_a = double.Parse(elements.ElementAt(0).Element("齿顶圆直径").Value) / 1000;
            double u = Math.Sqrt(((d_a * 1000) / (d_b * 1000)) * ((d_a * 1000) / (d_b * 1000)) - 1);   //滚动角
            double d = double.Parse(elements.ElementAt(0).Element("分度圆直径").Value) / 1000;
            double s = double.Parse(elements.ElementAt(0).Element("齿弦厚").Value) / 1000;
            double d_f = double.Parse(elements.ElementAt(0).Element("齿根圆直径").Value) / 1000;
            double C_CD = double.Parse(elements.ElementAt(0).Element("齿顶倒角").Value) / 1000;

            elements = from 螺旋角 in xe.Elements("设计参数")// where PInfo.Attribute("ID").Value == strID
                       select 螺旋角;
            double beta = double.Parse(elements.ElementAt(0).Element("螺旋角").Value);
            double luoju = Math.PI * d / Math.Tan(beta * Math.PI / 180);
            double B = double.Parse(elements.ElementAt(0).Element("齿宽").Value) / 1000;
            double Z = double.Parse(elements.ElementAt(0).Element("齿数").Value);
            string xuanxiang = elements.ElementAt(0).Element("旋向").Value;

            double numberOfLeftShaft = double.Parse(elements.ElementAt(0).Element("左轴段数").Value);
            double numberOfRightShaft = double.Parse(elements.ElementAt(0).Element("右轴段数").Value);
            double numberOfKeySeat = double.Parse(elements.ElementAt(0).Element("键槽数").Value);

            //SldWorks swApp;
            // IModelDoc2 swModel = default(IModelDoc2);
            ISelectionMgr swSelMgr = default(ISelectionMgr);
            Feature swFeat = default(Feature);
            HelixFeatureData swHelix = default(HelixFeatureData);
            bool bRet;
            bool boolstatus;

            swSelMgr = modDoc.SelectionManager as SelectionMgr;

            // swModel = (ModelDoc2)swApp.ActiveDoc;
            boolstatus = modDoc.Extension.SelectByID2("螺旋线", "REFERENCECURVES", 0, 0, 0, false, 0, null, 0);
            swFeat = swSelMgr.GetSelectedObject6(1, -1);
            swHelix = (HelixFeatureData)swFeat.GetDefinition();
            if (xuanxiang.Equals("L"))
            {
                swHelix.Clockwise = true;
            }
            else
            {
                swHelix.Clockwise = false;
            }
            bRet = swFeat.ModifyDefinition(swHelix, modDoc, null);

            IDimension myDimension = null;

            Parameter[] gearparameter = new Parameter[16];
            gearparameter[0].name = "基圆@草图1"; gearparameter[0].value = d_b;
            gearparameter[1].name = "U@草图1"; gearparameter[1].value = u * Math.PI / 180;
            gearparameter[2].name = "分度圆@草图1"; gearparameter[2].value = d;
            gearparameter[3].name = "齿弦厚@草图1"; gearparameter[3].value = s;
            gearparameter[4].name = "齿根圆@草图5"; gearparameter[4].value = d_f;
            gearparameter[5].name = "D3@螺旋线"; gearparameter[5].value = B + 0.001;
            gearparameter[6].name = "D1@齿根圆"; gearparameter[6].value = B + 0.001;
            gearparameter[7].name = "B@草图6"; gearparameter[7].value = B;
            gearparameter[8].name = "D1@齿廓阵列"; gearparameter[8].value = Z;
            gearparameter[9].name = "D1@草图6"; gearparameter[9].value = d_a + 0.002;
            gearparameter[10].name = "D2@草图6"; gearparameter[10].value = B + 1;
            gearparameter[11].name = "D4@螺旋线"; gearparameter[11].value = luoju;
            gearparameter[12].name = "D1@齿顶倒角"; gearparameter[12].value = C_CD;
            gearparameter[13].name = "D1@齿顶倒角阵列"; gearparameter[13].value = Z;
            gearparameter[14].name = "D1@草图7"; gearparameter[14].value = d_a;
            gearparameter[15].name = "D1@齿顶倒角1"; gearparameter[15].value = C_CD;
            for (int i = 0; i < gearparameter.Length; i++)
            {
                myDimension = (IDimension)modDoc.Parameter(gearparameter[i].name);
                myDimension.SystemValue = gearparameter[i].value;
            }

            string zzd;
            XElement zzdEle = xe.Element("左轴段");
            for (int i = 1; i <= numberOfLeftShaft; i++)
            {
                zzd = "左轴段" + i.ToString();
                elements = from 直径 in zzdEle.Elements(zzd)// where PInfo.Attribute("ID").Value == strID
                           select 直径;
                double D = double.Parse(elements.ElementAt(0).Element("直径").Value) / 1000;
                double L = double.Parse(elements.ElementAt(0).Element("长度").Value) / 1000;
                double C = double.Parse(elements.ElementAt(0).Element("倒角").Value) / 1000;
                double R = double.Parse(elements.ElementAt(0).Element("圆角").Value) / 1000;

                myDimension = (IDimension)modDoc.Parameter(@"D1@" + zzd + "草图");
                myDimension.SystemValue = D;
                myDimension = (IDimension)modDoc.Parameter(@"D1@" + zzd);
                myDimension.SystemValue = L;
                myDimension = (IDimension)modDoc.Parameter(@"D1@" + zzd + "倒角");
                myDimension.SystemValue = C;
                myDimension = (IDimension)modDoc.Parameter(@"D1@" + zzd + "圆角");
                myDimension.SystemValue = R;
            }
            XElement yzdEle = xe.Element("右轴段");
            string yzd;
            for (int i = 1; i <= numberOfRightShaft; i++)
            {
                yzd = "右轴段" + i.ToString();
                elements = from 直径 in yzdEle.Elements(yzd)// where PInfo.Attribute("ID").Value == strID
                           select 直径;
                double D = double.Parse(elements.ElementAt(0).Element("直径").Value) / 1000;
                double L = double.Parse(elements.ElementAt(0).Element("长度").Value) / 1000;
                double C = double.Parse(elements.ElementAt(0).Element("倒角").Value) / 1000;
                double R = double.Parse(elements.ElementAt(0).Element("圆角").Value) / 1000;

                myDimension = (IDimension)modDoc.Parameter(@"D1@" + yzd + "草图");
                myDimension.SystemValue = D;
                myDimension = (IDimension)modDoc.Parameter(@"D1@" + yzd);
                myDimension.SystemValue = L;
                myDimension = (IDimension)modDoc.Parameter(@"D1@" + yzd + "倒角");
                myDimension.SystemValue = C;
                myDimension = (IDimension)modDoc.Parameter(@"D1@" + yzd + "圆角");
                myDimension.SystemValue = R;
            }

            XElement keySeatEle = xe.Element("键槽");
            string keySeat;
            for (int i = 1; i <= numberOfKeySeat; i++)
            {
                keySeat = "键槽" + i.ToString();
                elements = from 直径 in keySeatEle.Elements(keySeat)// where PInfo.Attribute("ID").Value == strID
                           select 直径;
                double K = double.Parse(elements.ElementAt(0).Element("宽").Value) / 1000;
                double L = double.Parse(elements.ElementAt(0).Element("长度").Value) / 1000;
                double C = double.Parse(elements.ElementAt(0).Element("定位").Value) / 1000;
                double S = double.Parse(elements.ElementAt(0).Element("深").Value) / 1000;
                string weizhi = elements.ElementAt(0).Element("位置").Value;
                if (weizhi.Substring(0, 1).Equals("左"))
                {
                    elements = from 直径 in zzdEle.Elements(weizhi)// where PInfo.Attribute("ID").Value == strID
                               select 直径;
                }
                else
                {
                    elements = from 直径 in yzdEle.Elements(weizhi)// where PInfo.Attribute("ID").Value == strID
                               select 直径;
                }
                double D = double.Parse(elements.ElementAt(0).Element("直径").Value) / 1000;

                myDimension = (IDimension)modDoc.Parameter(@"D1@" + keySeat);
                myDimension.SystemValue = 0.5 * D - S;
                myDimension = (IDimension)modDoc.Parameter(@"D1@" + keySeat + "草图");
                myDimension.SystemValue = K;
                myDimension = (IDimension)modDoc.Parameter(@"D2@" + keySeat + "草图");
                myDimension.SystemValue = L;
                myDimension = (IDimension)modDoc.Parameter(@"D3@" + keySeat + "草图");
                myDimension.SystemValue = C;
            }
            modDoc.EditRebuild3();
            modDoc.ShowNamedView2("*Isometric", (int)swStandardViews_e.swTrimetricView);
            modDoc.ViewZoomtofit2();
            modDoc.Save3((int)swSaveAsOptions_e.swSaveAsOptions_Silent,
                   (int)swFileSaveError_e.swGenericSaveError,
                (int)swFileSaveWarning_e.swFileSaveWarning_RebuildError);
        }
Пример #2
0
        private void btnGearModelBuild_Click(object sender, EventArgs e)
        {
            //类变量,调研前要赋值
            //xlmFileName = @"C:\圆柱齿轮.xml";//齿轮的XML文件名字

            XElement xe = XElement.Load(strModelFileName);

            //读取齿轮模型文件名字
            IEnumerable<XElement> elements = from 齿轮名称 in xe.Elements("基本信息")// where PInfo.Attribute("ID").Value == strID
                                             select 齿轮名称;
            //MessageBox.Show(elements.ElementAt(0).Element("齿轮名称").Value);
            string modelFile = elements.ElementAt(0).Element("项目目录").Value + elements.ElementAt(0).Element("齿轮名称").Value;
            //利用齿轮模型文件名字,读取齿轮模型
            string partTemplate = SwAddin.iSwApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
            if ((partTemplate != null) && (partTemplate != ""))
            {
                modDoc = (IModelDoc2)SwAddin.iSwApp.OpenDoc6(modelFile,
                    (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "",
                    (int)swFileLoadError_e.swGenericError, (int)swFileLoadWarning_e.swFileLoadWarning_AlreadyOpen);
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("There is no part template available. Please check your options and make sure there is a part template selected, or select a new part template.");
            }

            elements = from 基圆直径 in xe.Elements("尺寸计算")// where PInfo.Attribute("ID").Value == strID
                       select 基圆直径;
            double d_b = double.Parse(elements.ElementAt(0).Element("基圆直径").Value) / 1000;          //基圆直径
            double d_a = double.Parse(elements.ElementAt(0).Element("齿顶圆直径").Value) / 1000;
            double u = Math.Sqrt(((d_a * 1000) / (d_b * 1000)) * ((d_a * 1000) / (d_b * 1000)) - 1);   //滚动角
            double d = double.Parse(elements.ElementAt(0).Element("分度圆直径").Value) / 1000;
            double s = double.Parse(elements.ElementAt(0).Element("齿弦厚").Value) / 1000;
            double d_f = double.Parse(elements.ElementAt(0).Element("齿根圆直径").Value) / 1000;
            double C_CD = double.Parse(elements.ElementAt(0).Element("齿顶倒角").Value) / 1000;

            elements = from 螺旋角 in xe.Elements("设计参数")// where PInfo.Attribute("ID").Value == strID
                       select 螺旋角;
            double beta = double.Parse(elements.ElementAt(0).Element("螺旋角").Value);
            double luoju = Math.PI * d / Math.Tan(beta * Math.PI / 180);
            double B = double.Parse(elements.ElementAt(0).Element("齿宽").Value) / 1000;
            double Z = double.Parse(elements.ElementAt(0).Element("齿数").Value);
            string xuanxiang = elements.ElementAt(0).Element("旋向").Value;

            elements = from 腹板深 in xe.Elements("腹板结构")// where PInfo.Attribute("ID").Value == strID
                       select 腹板深;
            double D_2 = double.Parse(elements.ElementAt(0).Element("腹板外径D2").Value) / 1000;
            double D_1 = double.Parse(elements.ElementAt(0).Element("腹板内径D1").Value) / 1000;
            double KJ_FB = double.Parse(elements.ElementAt(0).Element("腹板孔径dk").Value) / 1000;
            double N_FB = double.Parse(elements.ElementAt(0).Element("腹板孔数").Value);
            double D_FB = double.Parse(elements.ElementAt(0).Element("腹板深").Value) / 1000;
            double R_FB = double.Parse(elements.ElementAt(0).Element("腹板圆角").Value) / 1000;
            double C_FB = double.Parse(elements.ElementAt(0).Element("腹板倒角").Value) / 1000;

            elements = from 轮毂直径 in xe.Elements("轮毂结构")// where PInfo.Attribute("ID").Value == strID
                       select 轮毂直径;
            double D_LG = double.Parse(elements.ElementAt(0).Element("轮毂直径").Value) / 1000;
            double J_K = double.Parse(elements.ElementAt(0).Element("键宽").Value) / 1000;
            double J_S = double.Parse(elements.ElementAt(0).Element("键深").Value) / 1000;
            double C_LG = double.Parse(elements.ElementAt(0).Element("轮毂倒角").Value) / 1000;
            double R_LG = double.Parse(elements.ElementAt(0).Element("轮毂圆角").Value) / 1000;

            //SldWorks swApp;
            // IModelDoc2 swModel = default(IModelDoc2);
            ISelectionMgr swSelMgr = default(ISelectionMgr);
            Feature swFeat = default(Feature);
            HelixFeatureData swHelix = default(HelixFeatureData);
            bool bRet;
            bool boolstatus;

            swSelMgr = modDoc.SelectionManager as SelectionMgr;

            // swModel = (ModelDoc2)swApp.ActiveDoc;
            boolstatus = modDoc.Extension.SelectByID2("螺旋线", "REFERENCECURVES", 0, 0, 0, false, 0, null, 0);
            swFeat = swSelMgr.GetSelectedObject6(1, -1);
            swHelix = (HelixFeatureData)swFeat.GetDefinition();
            if (xuanxiang.Equals("L"))
            {
                swHelix.Clockwise = true;
            }
            else
            {
                swHelix.Clockwise = false;
            }
            bRet = swFeat.ModifyDefinition(swHelix, modDoc, null);

            IDimension myDimension = null;

            Parameter[] gearparameter = new Parameter[34];

            gearparameter[0].name = "基圆@草图1"; gearparameter[0].value = d_b;
            gearparameter[1].name = "U@草图1"; gearparameter[1].value = u * Math.PI / 180;
            gearparameter[2].name = "分度圆@草图1"; gearparameter[2].value = d;
            gearparameter[3].name = "齿弦厚@草图1"; gearparameter[3].value = s;
            gearparameter[4].name = "齿根圆@草图5"; gearparameter[4].value = d_f;
            gearparameter[5].name = "D3@螺旋线"; gearparameter[5].value = B + 0.001;
            gearparameter[6].name = "D1@齿根圆"; gearparameter[6].value = B + 0.001;
            gearparameter[7].name = "B@草图6"; gearparameter[7].value = B;
            gearparameter[8].name = "D1@齿廓阵列"; gearparameter[8].value = Z;
            gearparameter[9].name = "D1@草图6"; gearparameter[9].value = d_a + 0.002;
            gearparameter[10].name = "D4@螺旋线"; gearparameter[10].value = luoju;
            gearparameter[11].name = "D1@齿顶倒角"; gearparameter[11].value = C_CD;
            gearparameter[12].name = "腹板外径@草图7"; gearparameter[12].value = D_2;
            gearparameter[13].name = "腹板内径@草图7"; gearparameter[13].value = D_1;
            gearparameter[14].name = "D1@草图7"; gearparameter[14].value = (D_1 + D_2) / 2;
            gearparameter[15].name = "D2@草图7"; gearparameter[15].value = (180 / N_FB) * Math.PI / 180;
            gearparameter[16].name = "D1@腹板"; gearparameter[16].value = D_FB;
            gearparameter[17].name = "D1@腹板圆角"; gearparameter[17].value = R_FB;
            gearparameter[18].name = "D1@腹板倒角"; gearparameter[18].value = C_FB;
            gearparameter[19].name = "D1@腹板孔阵列"; gearparameter[19].value = N_FB;
            gearparameter[20].name = "D2@草图6"; gearparameter[20].value = B + 1;
            gearparameter[21].name = "D3@草图7"; gearparameter[21].value = KJ_FB;
            gearparameter[22].name = "D1@齿顶倒角阵列"; gearparameter[22].value = Z;
            gearparameter[23].name = "D1@草图8"; gearparameter[23].value = D_LG;
            gearparameter[24].name = "D2@草图8"; gearparameter[24].value = J_K;
            gearparameter[25].name = "D3@草图8"; gearparameter[25].value = J_S;
            gearparameter[26].name = "D1@轮毂倒角"; gearparameter[26].value = C_LG;
            gearparameter[27].name = "D1@轮毂圆角"; gearparameter[27].value = R_LG;
            gearparameter[28].name = "D1@草图9"; gearparameter[28].value = d_f / 2 - 0.001;
            gearparameter[29].name = "D2@草图9"; gearparameter[29].value = d_a / 2;
            gearparameter[30].name = "D3@草图9"; gearparameter[30].value = B;
            gearparameter[31].name = "D1@草图10"; gearparameter[31].value = d_f;
            gearparameter[32].name = "D3@阵列1"; gearparameter[32].value = (180 / N_FB) * Math.PI / 180;
            gearparameter[33].name = "D1@齿顶倒角1"; gearparameter[33].value = C_CD;
            for (int i = 0; i < gearparameter.Length; i++)
            {
                myDimension = (IDimension)modDoc.Parameter(gearparameter[i].name);
                myDimension.SetSystemValue3(gearparameter[i].value, (int)swInConfigurationOpts_e.swAllConfiguration, null);
            }
            modDoc.EditRebuild3();
            modDoc.ShowNamedView2("*Isometric", (int)swStandardViews_e.swTrimetricView);
            modDoc.ViewZoomtofit2();
            modDoc.Save3((int)swSaveAsOptions_e.swSaveAsOptions_Silent, (int)swFileSaveError_e.swGenericSaveError, (int)swFileSaveWarning_e.swFileSaveWarning_RebuildError);
        }