/// <summary>
        ///     Подключиться к программе
        /// </summary>
        public void ConnectToApp()
        {
            Type solidWorksType = null;

            foreach (var number in _appSettings.ApiNumbers)
            {
                solidWorksType = Type.GetTypeFromProgID($"SldWorks.Application.{number}");
                if (solidWorksType != null)
                {
                    break;
                }
            }

            if (solidWorksType == null)
            {
                throw new ExternalException(string.Join(", ", _appSettings.ApiNumbers));
            }

            var appInstance = Activator.CreateInstance(solidWorksType);

            _app         = (SldWorks)appInstance;
            _app.Visible = true;

            _app.NewPart();
            _document = _app.IActiveDoc2;

            BuildedPartFiguresCount = 0;
        }
        /// <summary>
        /// The main code of the SW part creation and the rest
        /// </summary>

        public static void createSWPart()
        {
            swApp = SolidWorksSingleton.swApp;

            if (CreatePart.exisCheck == 1)
            {
                //Open existing part
                swApp.OpenDoc6(CreatePart.pthfileName, 1, 256, "", 0, 0);
            }
            else
            {
                //create a part
                swPart = swApp.NewPart();
            }

            //activate part
            swModel = swApp.ActiveDoc;

            assignModelProperties();

            createPartNumber(1);

            //Save the part under a specific number
            swModel.SaveAs3(saveName, 0, 1);
        }
Example #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            double r1 = Convert.ToDouble(textBox1.Text) / 1e3;
            double r2 = Convert.ToDouble(textBox2.Text) / 1e3;
            double h  = Convert.ToDouble(textBox3.Text) / 1e3;

            SldWorks sw = new SldWorks();

            sw.Visible = true;
            PartDoc   Part  = sw.NewPart();
            ModelDoc2 Model = sw.ActiveDoc;

            Model.InsertSketch2(true);

            Model.SelectByID("Спереди", "PLANE", 0, 0, 0); // Выделяем плоскость на которой будем рисовать

            Model.InsertSketch2(true);                     // Добавляем эскиз
            Model.CreateCircleByRadius2(0, 0, 0, r1);      // рисуем окружность 1
            Model.CreateCircleByRadius2(0, 0, 0, r2);      // рисуем окружность 2
            Model.FeatureBoss2(true, false, false,
                               0, 0, h, 0, true, false, true, false,
                               0, 0, false, false, false, false);  // Бобышка - вытянуть

            /*Model.CreateEllipse2(0, 0, 0, 1, 0, 0, 0, 2, 0);
             * Model.FeatureBoss2(true, false, false, 0, 0, 1, 0, true, false, true, false, 0.5, 0, false, false, false, false);*/
        }
Example #4
0
 //создает проект детали
 public void CreateDoc()
 {
     swApp = new SldWorks {
         Visible = true
     };
     swApp.NewPart();
     swDoc = swApp.GetFirstDocument();
     swDoc.SketchManager.InsertSketch(true);
 }
Example #5
0
        public static ModelDoc2 WorkSolid(SldWorks SwApp = null)
        {
            ModelDoc2 swModel = null;

            try
            {
                if (SwApp == null)
                {
                    Process[] processers = Process.GetProcessesByName("SLDWORKS");
                    foreach (Process process in processers)
                    {
                        process.CloseMainWindow();
                        process.Kill();
                    }

                    //створення процесу
                    object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("SldWorks.Application"));
                    //Guid myGuid1 = new Guid("0D825E02-9000-4D82-B4AB-D6BDC2872797");
                    //object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromCLSID(myGuid1));

                    SwApp         = (SldWorks)processSW;
                    SwApp.Visible = true;
                }

                //створення моделі
                int    errors = 0, warnings = 0;
                String Document = "C:\\ProgramData\\SolidWorks\\SolidWorks 2013\\templates\\Part.prtdot";
                SwApp.OpenDoc6(Document, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
                swModel = (ModelDoc2)SwApp.NewPart();
                SwApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swSketchInference, false);
                SwApp = null;
            }
            catch (Exception ex)
            {
                string erorr = ex.ToString();
            }
            return(swModel);
        }
Example #6
0
        SldWorks SwApp1;    // объект приложения Solidworks



        private void buttonStartSolid_Click(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("SLDWORKS 2016");
            foreach (Process process in processes)
            {
                process.CloseMainWindow(); // если процесс запущен, убивает процесс и запускает его снова
                process.Kill();
            }

            object processseSW = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("SldWorks.Application"));

            SwApp1             = (SldWorks)processseSW;
            SwApp1.UserControl = true;
            SwApp1.Visible     = true;

            MessageBox.Show("Solidworks запущен. Создайте новый документ детали.");



            SwApp1.NewPart(); //документ
            //передать интфейс с деталью в ImodelDoc
            SWmodel = SwApp1.IActiveDoc2;
        }
Example #7
0
        /// <summary>
        /// Создание изолятора
        /// </summary>
        public static void CreateIsolator(double isolatorLength, SldWorks swApp, ModelDoc2 swModel, List <string> detailNames, string pathName)
        {
            var isolatorDist = 0.01;

            string modelName = pathName + "Изолятор.sldprt";

            swApp.NewPart();
            swModel = swApp.IActiveDoc2;

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, 0.008);
            swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, isolatorLength, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);

            // Фаска
            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, 0.006);
            swModel.FeatureManager.FeatureCut3(true, true, true, 0, 0, isolatorDist, isolatorDist, true, false, false, false, _dAngle, _dAngle, false, false, false, false, false, true, true, true, true, false, 0, 0, false);

            swModel.Extension.SelectByID2("", "FACE", 0, 0, 0, false, 0, null, 0);

            swModel.SaveAs(modelName);
            detailNames.Add(modelName);
        }
        private void Button_generateFEM_Click(object sender, RoutedEventArgs e)
        {
            #region Variable definition

            int              m;     //how many cells
            int              Count; // how many data
            double           width, thickness, l1, l2, l3, F1, F2, F3;
            double           l4, l1_, l2_, l3_, l4_, cell_length, cell_height;
            double           x_o            = 0.0;
            double           y_o            = 0.0;
            double           x_o_           = 0.0;
            double           y_o_           = 0.0;
            const double     MeshEleSize    = 2.0;
            const double     MeshTol        = 0.1;
            string           strMaterialLib = null;
            object[]         Disp           = null;
            object[]         Stress         = null;
            ModelDoc2        swModel        = null;
            SelectionMgr     selectionMgr   = null;
            CosmosWorks      COSMOSWORKS    = null;
            CwAddincallback  COSMOSObject   = default(CwAddincallback);
            CWModelDoc       ActDoc         = default(CWModelDoc);
            CWStudyManager   StudyMngr      = default(CWStudyManager);
            CWStudy          Study          = default(CWStudy);
            CWSolidManager   SolidMgr       = default(CWSolidManager);
            CWSolidBody      SolidBody      = default(CWSolidBody);
            CWSolidComponent SolidComp      = default(CWSolidComponent);


            CWForce cwForce = default(CWForce);
            CWLoadsAndRestraintsManager LBCMgr = default(CWLoadsAndRestraintsManager);
            CWResults CWFeatobj = default(CWResults);
            bool      isSelected;
            float     maxDisp   = 0.0f;
            float     maxStress = 0.0f;
            int       intStatus = 0;
            int       errors    = 0;
            int       errCode   = 0;
            int       warnings  = 0;



            int ran_m, ran_width, ran_thickness, ran_l1, ran_l2, ran_l3, ran_F1, ran_F2, ran_F3;
            Excel.Application exlApp;
            Excel.Workbook    exlBook;
            Excel.Worksheet   exlSheet;



            #endregion

            #region read data from UI
            try
            {
                m         = int.Parse(textBox_hm_cells.Text);
                width     = double.Parse(textBox_width.Text) / 1000.0;
                thickness = double.Parse(textBox_thickness.Text) / 1000.0;
                l1        = double.Parse(textBox_l1.Text) / 1000.0;
                l2        = double.Parse(textBox_l2.Text) / 1000.0;
                l3        = double.Parse(textBox_l3.Text) / 1000.0;
                F1        = double.Parse(textBox_F1.Text);
                F2        = double.Parse(textBox_F2.Text);
                F3        = double.Parse(textBox_F3.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("input format error, check input data");
                return;
            }

            l4          = l2;
            l1_         = l1 - 2 * width;
            l2_         = l2;
            l3_         = l3 + 2 * width;
            l4_         = l4;
            cell_length = l1 + l3;
            cell_height = l2 + width;
            #endregion

            #region geometrie
            Console.WriteLine("Start creating new Germetrie");
            swModel = swApp.NewPart();
            swModel.Extension.SelectByID("前视基准面", "PLANE", 0, 0, 0, false, 1, null);
            swModel.InsertSketch2(true);
            #region sketch
            for (int i = 0; i < m; i++)
            {
                if (i == 0)
                {
                    swModel.SketchManager.CreateLine(0, 0, 0, l1 - width, 0, 0);
                    swModel.SketchManager.CreateLine(l1 - width, 0, 0, l1 - width, -l2, 0);
                    swModel.SketchManager.CreateLine(l1 - width, -l2, 0, l1 + l3 - width, -l2, 0);
                    swModel.SketchManager.CreateLine(l1 + l3 - width, -l2, 0, l1 + l3 - width, 0, 0);

                    swModel.SketchManager.CreateLine(0, 0, 0, 0, -width, 0);
                    swModel.SketchManager.CreateLine(0, -width, 0, l1_, -width, 0);
                    swModel.SketchManager.CreateLine(l1_, -width, 0, l1_, -(width + l2_), 0);
                    swModel.SketchManager.CreateLine(l1_, -(width + l2_), 0, l1_ + l3_, -(width + l2_), 0);
                    swModel.SketchManager.CreateLine(l1_ + l3_, -(width + l2_), 0, l1_ + l3_, -width, 0);
                }
                else
                {
                    x_o  = i * cell_length - width;
                    y_o  = 0;
                    x_o_ = x_o + width;
                    y_o_ = y_o - width;

                    swModel.SketchManager.CreateLine(x_o, y_o, 0, x_o + l1, 0, 0);
                    swModel.SketchManager.CreateLine(x_o + l1, 0, 0, x_o + l1, -l2, 0);
                    swModel.SketchManager.CreateLine(x_o + l1, -l2, 0, x_o + l1 + l3, -l2, 0);
                    swModel.SketchManager.CreateLine(x_o + l1 + l3, -l2, 0, x_o + l1 + l3, 0, 0);

                    swModel.SketchManager.CreateLine(x_o_, y_o_, 0, x_o_ + l1_, y_o_, 0);
                    swModel.SketchManager.CreateLine(x_o_ + l1_, y_o_, 0, x_o_ + l1_, y_o_ - l2_, 0);
                    swModel.SketchManager.CreateLine(x_o_ + l1_, y_o_ - l2_, 0, x_o_ + l1_ + l3_, y_o_ - l2_, 0);
                    swModel.SketchManager.CreateLine(x_o_ + l1_ + l3_, y_o_ - l2_, 0, x_o_ + l1_ + l3_, y_o_, 0);
                }
            }
            swModel.SketchManager.CreateLine(x_o_ + l1_ + l3_, y_o_, 0, x_o_ + l1_ + l3_, 0, 0);
            swModel.SketchManager.CreateLine(x_o_ + l1_ + l3_, 0, 0, x_o + l1 + l3, 0, 0);

            #endregion
            swModel.FeatureManager.FeatureExtrusion2(
                true, false, false, 0, 0, thickness, 0, false, false, false, false, 0, 0, false, false, false, false, true, true, true, 0, 0, true
                );

            swModel.SaveAsSilent("D:\\TUD\\7.Semeter\\SA\\SA_code\\c#\\W_Form_analyse_FEM_NN_GUI\\Geometrie.sldprt", true);
            swApp.CloseAllDocuments(true);
            swApp.OpenDoc("D:\\TUD\\7.Semeter\\SA\\SA_code\\c#\\W_Form_analyse_FEM_NN_GUI\\Geometrie.sldprt", (int)swOpenDocOptions_e.swOpenDocOptions_Silent);
            Console.WriteLine("Geometrie success");


            #endregion

            #region simulaiton
            string path_to_cosworks_dll = @"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\Simulation\cosworks.dll";
            errors       = swApp.LoadAddIn(path_to_cosworks_dll);
            COSMOSObject = (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");
            try
            {
                COSMOSWORKS = (CosmosWorks)COSMOSObject.CosmosWorks;
            }
            catch (Exception)
            {
                Console.WriteLine("something wrong in Simulaiton Add In, start a new one");
                swApp.CloseAllDocuments(true);

                return;
            }


            COSMOSWORKS = COSMOSObject.CosmosWorks;
            //Get active document
            ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;

            //Create new static study
            StudyMngr = (CWStudyManager)ActDoc.StudyManager;
            Study     = (CWStudy)StudyMngr.CreateNewStudy("static study", (int)swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, 0, out errCode);

            //Add materials
            //get MaterialLib
            strMaterialLib = swApp.GetExecutablePath() + "\\lang\\english\\sldmaterials\\solidworks materials.sldmat";
            SolidMgr       = Study.SolidManager;
            SolidComp      = SolidMgr.GetComponentAt(0, out errCode);
            SolidBody      = SolidComp.GetSolidBodyAt(0, out errCode);
            intStatus      = SolidBody.SetLibraryMaterial(strMaterialLib, "AISI 1020");

            //fixed restraints
            LBCMgr = Study.LoadsAndRestraintsManager;

            swModel = (ModelDoc2)swApp.ActiveDoc;
            swModel.ShowNamedView2("", (int)swStandardViews_e.swIsometricView);

            selectionMgr = (SelectionMgr)swModel.SelectionManager;
            isSelected   = swModel.Extension.SelectByID2("", "FACE", 0, -width / 2.0, thickness / 2.0, false, 0, null, 0);
            if (isSelected)
            {
                object      selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1);
                object[]    fixedFaces   = { selectedFace };
                CWRestraint restraint    = (CWRestraint)LBCMgr.AddRestraint((int)swsRestraintType_e.swsRestraintTypeFixed, fixedFaces, null, out errCode);
            }
            swModel.ClearSelection2(true);

            //add force
            selectionMgr = (SelectionMgr)swModel.SelectionManager;
            isSelected   = swModel.Extension.SelectByID2("", "FACE", x_o + l1 + l3 + (width / 2.0), 0, thickness / 2.0, false, 0, null, 0);
            if (isSelected)
            {
                object   selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1);
                object[] forceAdd     = { selectedFace };
                selectionMgr = (SelectionMgr)swModel.SelectionManager;
                swModel.Extension.SelectByID2("", "FACE", x_o + l1 + l3 + width, -(l4 + width) / 2.0, thickness / 2.0, false, 0, null, 0);
                object   selectedFaceToForceDir = (object)selectionMgr.GetSelectedObject6(1, -1);
                double[] distValue  = null;
                double[] forceValue = null;
                double[] Force      = { F2, F3, F1 };
                cwForce = (CWForce)LBCMgr.AddForce3((int)swsForceType_e.swsForceTypeForceOrMoment, (int)swsSelectionType_e.swsSelectionFaceEdgeVertexPoint,
                                                    2, 0, 0, 0,
                                                    (distValue),
                                                    (forceValue),
                                                    false, true,
                                                    (int)swsBeamNonUniformLoadDef_e.swsTotalLoad,
                                                    0, 7, 0.0,
                                                    Force,
                                                    false, false,
                                                    (forceAdd),
                                                    (selectedFaceToForceDir),
                                                    false, out errCode); //i have tried to figure out these arguments for one day, keep them and dont't change them.
                                                                         //the way to check cwForce : cwForce.GetForceComponentValues()
                                                                         //ForceComponet: [int b1, // 1 if x-direction hat Force-komponent, else 0
                                                                         //                int b2, // 1 if y-direction hat Force-komponent, else 0
                                                                         //                int b3, // 1 if z-direction hat Force-komponent, else 0
                                                                         //                double d1, // Force-komponent in x
                                                                         //                double d2, // Force-komponent in y
                                                                         //                double d3 // Force-komponent in z
                                                                         //                          ]
                                                                         //PS: the definition of xyz seems like not the same as the global XYZ system in SW.
                swModel.ClearSelection2(true);

                //meshing
                CWMesh CWMeshObj = default(CWMesh);
                CWMeshObj            = Study.Mesh;
                CWMeshObj.MesherType = (int)swsMesherType_e.swsMesherTypeStandard;
                CWMeshObj.Quality    = (int)swsMeshQuality_e.swsMeshQualityDraft;
                errCode   = Study.CreateMesh(0, MeshEleSize, MeshTol);
                CWMeshObj = null;

                //run analysis
                errCode = Study.RunAnalysis();
                if (errCode != 0)
                {
                    Console.WriteLine(string.Format("RunAnalysis errCode = {0}", errCode));
                    Console.WriteLine(string.Format("RunAnalysis failed"));

                    errors = swApp.UnloadAddIn(path_to_cosworks_dll);
                    swApp.CloseAllDocuments(true);
                    Console.WriteLine(string.Format("please start a new one"));
                    return;
                }
                Console.WriteLine("RunAnalysis successed, ready to get results");

                //get results
                CWFeatobj = Study.Results;
                //get max von Mieses stress
                Stress = (object[])CWFeatobj.GetMinMaxStress((int)swsStressComponent_e.swsStressComponentVON,
                                                             0, 0, null,
                                                             (int)swsStrengthUnit_e.swsStrengthUnitNewtonPerSquareMillimeter,
                                                             out errCode);
                maxStress = (float)Stress[3]; //Stress: {node_with_minimum_stress, minimum_stress, node_with_maximum_stress, maximum_stress}
                Console.WriteLine(maxStress);
                if (maxStress >= 351.6)
                {
                    Console.WriteLine("out of yield stress, start a new example");
                    errors = swApp.UnloadAddIn(path_to_cosworks_dll);
                    swApp.CloseAllDocuments(true);
                    textBox_FEM_result.Text = "out of yield stress";
                    return;
                }
                //get max URES displacement
                Disp = (object[])CWFeatobj.GetMinMaxDisplacement((int)swsDisplacementComponent_e.swsDisplacementComponentURES,
                                                                 0, null,
                                                                 (int)swsLinearUnit_e.swsLinearUnitMillimeters,
                                                                 out errCode);
                maxDisp   = (float)Disp[3]; //Disp: {node_with_minimum_displacement, minimum_displacement, node_with_maximum_displacement, maximum_displacement}
                CWFeatobj = null;
                Console.WriteLine(string.Format("max Displacement: {0:f4} mm", maxDisp));
                textBox_FEM_result.Text = maxDisp.ToString();
            }
            else
            {
                Console.WriteLine("not selected");
            }
            #endregion
            errors = swApp.UnloadAddIn(path_to_cosworks_dll);
            swApp.CloseAllDocuments(true);
        }
        private void button_create_Geom_Click(object sender, RoutedEventArgs e)
        {
            #region a test example, not important
            m         = 9;
            width     = 8 / 1000.0;
            thickness = 1 / 1000.0;
            l1        = 30 / 1000.0;
            l2        = 16 / 1000.0;
            l3        = 21 / 1000.0;
            F1        = 1;
            F2        = 37;
            F3        = 49;

            l4          = l2;
            l1_         = l1 - 2 * width;
            l2_         = l2;
            l3_         = l3 + 2 * width;
            l4_         = l4;
            cell_length = l1 + l3;
            cell_height = l2 + width;
            #endregion

            Count = int.Parse(textBox_count.Text);
            Random randomSeed       = new Random();
            Random random_m         = new Random(randomSeed.Next());
            Random random_width     = new Random(randomSeed.Next());
            Random random_thickness = new Random(randomSeed.Next());
            Random random_l1        = new Random(randomSeed.Next());
            Random random_l2        = new Random(randomSeed.Next());
            Random random_l3        = new Random(randomSeed.Next());
            Random random_F1        = new Random(randomSeed.Next());
            Random random_F2        = new Random(randomSeed.Next());
            Random random_F3        = new Random(randomSeed.Next());

            for (int j = 0; j < Count; j++)
            {
                Console.WriteLine(string.Format("---------------------------------now No.{0}", j));

                #region generate random Data
                try
                {
                    /* change to double
                     * ran_m = random_m.Next(int.Parse(textBox_hm_cells_min.Text), int.Parse(textBox_hm_cellls_max.Text));
                     * ran_width = random_width.Next(int.Parse(textBox_width_min.Text), int.Parse(textBox_width_max.Text));
                     * ran_thickness = random_thickness.Next(int.Parse(textBox_thickness_min.Text), int.Parse(textBox_thickness_max.Text));
                     * ran_l1 = random_l1.Next(int.Parse(textBox_l1_min.Text), int.Parse(textBox_l1_max.Text));
                     * ran_l2 = random_l2.Next(int.Parse(textBox_l2_min.Text), int.Parse(textBox_l2_max.Text));
                     * ran_l3 = random_l3.Next(int.Parse(textBox_l3_min.Text), int.Parse(textBox_l3_max.Text));
                     * ran_F1 = random_F1.Next(int.Parse(textBox_F1_min.Text), int.Parse(textBox_F1_max.Text));
                     * ran_F2 = random_F2.Next(int.Parse(textBox_F2_min.Text), int.Parse(textBox_F2_max.Text));
                     * ran_F3 = random_F3.Next(int.Parse(textBox_F3_min.Text), int.Parse(textBox_F3_max.Text));
                     */
                    ran_m         = random_m.Next(int.Parse(textBox_hm_cells_min.Text), int.Parse(textBox_hm_cellls_max.Text));
                    ran_width     = random_width.NextDouble() * (int.Parse(textBox_width_max.Text) - int.Parse(textBox_width_min.Text)) + double.Parse(textBox_width_min.Text);
                    ran_thickness = random_thickness.NextDouble() * (int.Parse(textBox_thickness_max.Text) - int.Parse(textBox_thickness_min.Text)) + double.Parse(textBox_thickness_min.Text);
                    ran_l1        = random_l1.NextDouble() * (int.Parse(textBox_l1_max.Text) - int.Parse(textBox_l1_min.Text)) + double.Parse(textBox_l1_min.Text);
                    ran_l2        = random_l2.NextDouble() * (int.Parse(textBox_l2_max.Text) - int.Parse(textBox_l2_min.Text)) + double.Parse(textBox_l2_min.Text);
                    ran_l3        = random_l3.NextDouble() * (int.Parse(textBox_l3_max.Text) - int.Parse(textBox_l3_min.Text)) + double.Parse(textBox_l3_min.Text);
                    ran_F1        = random_F1.NextDouble() * (int.Parse(textBox_F1_max.Text) - int.Parse(textBox_F1_min.Text)) + double.Parse(textBox_F1_min.Text);
                    ran_F2        = random_F2.NextDouble() * (int.Parse(textBox_F2_max.Text) - int.Parse(textBox_F2_min.Text)) + double.Parse(textBox_F2_min.Text);
                    ran_F3        = random_F3.NextDouble() * (int.Parse(textBox_F3_max.Text) - int.Parse(textBox_F3_min.Text)) + double.Parse(textBox_F3_min.Text);
                }
                catch (Exception)
                {
                    Console.WriteLine("Input Format Error");
                    return;
                }


                m         = ran_m;
                width     = (double)(ran_width / 1000.0);
                thickness = (double)(ran_thickness / 1000.0);
                l1        = (double)(ran_l1 / 1000.0);
                l2        = (double)(ran_l2 / 1000.0);
                l3        = (double)(ran_l3 / 1000.0);

                F1 = (double)ran_F1;
                F2 = (double)ran_F2;
                F3 = (double)ran_F3;
                F3 = 0;

                l4          = l2;
                l1_         = l1 - 2 * width;
                l2_         = l2;
                l3_         = l3 + 2 * width;
                l4_         = l4;
                cell_length = l1 + l3;
                cell_height = l2 + width;


                #endregion



                Console.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", m, width, thickness, l1, l2, l3));

                #region geometrie
                Console.WriteLine("Start creating new Germetrie");

                try
                {
                    swModel = swApp.NewPart();
                }
                catch (Exception)
                {
                    Console.WriteLine("starting SolidWorks");
                    try
                    {
                        swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
                    }
                    catch (Exception)
                    {
                        swApp         = new SldWorks();
                        swApp.Visible = false;
                    }

                    //get MaterialLib
                    strMaterialLib = swApp.GetExecutablePath() + "\\lang\\english\\sldmaterials\\solidworks materials.sldmat";

                    Console.WriteLine("SolidWorks successfully started");
                    j--;
                    continue;
                }

                swModel.Extension.SelectByID("前视基准面", "PLANE", 0, 0, 0, false, 1, null);
                swModel.InsertSketch2(true);
                #region sketch
                for (int i = 0; i < m; i++)
                {
                    if (i == 0)
                    {
                        swModel.SketchManager.CreateLine(0, 0, 0, l1 - width, 0, 0);
                        swModel.SketchManager.CreateLine(l1 - width, 0, 0, l1 - width, -l2, 0);
                        swModel.SketchManager.CreateLine(l1 - width, -l2, 0, l1 + l3 - width, -l2, 0);
                        swModel.SketchManager.CreateLine(l1 + l3 - width, -l2, 0, l1 + l3 - width, 0, 0);

                        swModel.SketchManager.CreateLine(0, 0, 0, 0, -width, 0);
                        swModel.SketchManager.CreateLine(0, -width, 0, l1_, -width, 0);
                        swModel.SketchManager.CreateLine(l1_, -width, 0, l1_, -(width + l2_), 0);
                        swModel.SketchManager.CreateLine(l1_, -(width + l2_), 0, l1_ + l3_, -(width + l2_), 0);
                        swModel.SketchManager.CreateLine(l1_ + l3_, -(width + l2_), 0, l1_ + l3_, -width, 0);
                    }
                    else
                    {
                        x_o  = i * cell_length - width;
                        y_o  = 0;
                        x_o_ = x_o + width;
                        y_o_ = y_o - width;

                        swModel.SketchManager.CreateLine(x_o, y_o, 0, x_o + l1, 0, 0);
                        swModel.SketchManager.CreateLine(x_o + l1, 0, 0, x_o + l1, -l2, 0);
                        swModel.SketchManager.CreateLine(x_o + l1, -l2, 0, x_o + l1 + l3, -l2, 0);
                        swModel.SketchManager.CreateLine(x_o + l1 + l3, -l2, 0, x_o + l1 + l3, 0, 0);

                        swModel.SketchManager.CreateLine(x_o_, y_o_, 0, x_o_ + l1_, y_o_, 0);
                        swModel.SketchManager.CreateLine(x_o_ + l1_, y_o_, 0, x_o_ + l1_, y_o_ - l2_, 0);
                        swModel.SketchManager.CreateLine(x_o_ + l1_, y_o_ - l2_, 0, x_o_ + l1_ + l3_, y_o_ - l2_, 0);
                        swModel.SketchManager.CreateLine(x_o_ + l1_ + l3_, y_o_ - l2_, 0, x_o_ + l1_ + l3_, y_o_, 0);
                    }
                }
                swModel.SketchManager.CreateLine(x_o_ + l1_ + l3_, y_o_, 0, x_o_ + l1_ + l3_, 0, 0);
                swModel.SketchManager.CreateLine(x_o_ + l1_ + l3_, 0, 0, x_o + l1 + l3, 0, 0);

                #endregion
                swModel.FeatureManager.FeatureExtrusion2(
                    true, false, false, 0, 0, thickness, 0, false, false, false, false, 0, 0, false, false, false, false, true, true, true, 0, 0, true
                    );

                swModel.SaveAsSilent("D:\\TUD\\7.Semeter\\SA\\SA_code\\c#\\W_Form_analyse_get_TrainingDaten\\Geometrie.sldprt", true);
                swApp.CloseAllDocuments(true);
                swApp.OpenDoc("D:\\TUD\\7.Semeter\\SA\\SA_code\\c#\\W_Form_analyse_get_TrainingDaten\\Geometrie.sldprt", (int)swOpenDocOptions_e.swOpenDocOptions_Silent);
                Console.WriteLine("Geometrie success");


                #endregion

                #region simulaiton
                string path_to_cosworks_dll = @"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\Simulation\cosworks.dll";
                errors       = swApp.LoadAddIn(path_to_cosworks_dll);
                COSMOSObject = (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");
                try
                {
                    COSMOSWORKS = (CosmosWorks)COSMOSObject.CosmosWorks;
                }
                catch (Exception)
                {
                    Console.WriteLine("something wrong in Simulaiton Add In, start a new one");
                    continue;
                }


                COSMOSWORKS = COSMOSObject.CosmosWorks;
                //Get active document
                ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;

                //Create new static study
                StudyMngr = (CWStudyManager)ActDoc.StudyManager;
                Study     = (CWStudy)StudyMngr.CreateNewStudy("static study", (int)swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, 0, out errCode);

                //Add materials
                SolidMgr  = Study.SolidManager;
                SolidComp = SolidMgr.GetComponentAt(0, out errCode);
                SolidBody = SolidComp.GetSolidBodyAt(0, out errCode);
                intStatus = SolidBody.SetLibraryMaterial(strMaterialLib, "AISI 1020");

                //fixed restraints
                LBCMgr = Study.LoadsAndRestraintsManager;

                swModel = (ModelDoc2)swApp.ActiveDoc;
                swModel.ShowNamedView2("", (int)swStandardViews_e.swIsometricView);

                selectionMgr = (SelectionMgr)swModel.SelectionManager;
                isSelected   = swModel.Extension.SelectByID2("", "FACE", 0, -width / 2.0, thickness / 2.0, false, 0, null, 0);
                if (isSelected)
                {
                    object      selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1);
                    object[]    fixedFaces   = { selectedFace };
                    CWRestraint restraint    = (CWRestraint)LBCMgr.AddRestraint((int)swsRestraintType_e.swsRestraintTypeFixed, fixedFaces, null, out errCode);
                }
                swModel.ClearSelection2(true);

                //add force
                selectionMgr = (SelectionMgr)swModel.SelectionManager;
                isSelected   = swModel.Extension.SelectByID2("", "FACE", x_o + l1 + l3 + (width / 2.0), 0, thickness / 2.0, false, 0, null, 0);
                if (isSelected)
                {
                    object   selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1);
                    object[] forceAdd     = { selectedFace };
                    selectionMgr = (SelectionMgr)swModel.SelectionManager;
                    swModel.Extension.SelectByID2("", "FACE", x_o + l1 + l3 + width, -(l4 + width) / 2.0, thickness / 2.0, false, 0, null, 0);
                    object   selectedFaceToForceDir = (object)selectionMgr.GetSelectedObject6(1, -1);
                    double[] distValue  = null;
                    double[] forceValue = null;
                    double[] Force      = { F2, F3, F1 };
                    cwForce = (CWForce)LBCMgr.AddForce3((int)swsForceType_e.swsForceTypeForceOrMoment, (int)swsSelectionType_e.swsSelectionFaceEdgeVertexPoint,
                                                        2, 0, 0, 0,
                                                        (distValue),
                                                        (forceValue),
                                                        false, true,
                                                        (int)swsBeamNonUniformLoadDef_e.swsTotalLoad,
                                                        0, 7, 0.0,
                                                        Force,
                                                        false, false,
                                                        (forceAdd),
                                                        (selectedFaceToForceDir),
                                                        false, out errCode); //i have tried to figure out these arguments for one day, keep them and dont't change them.
                                                                             //the way to check cwForce : cwForce.GetForceComponentValues()
                                                                             //ForceComponet: [int b1, // 1 if x-direction hat Force-komponent, else 0
                                                                             //                int b2, // 1 if y-direction hat Force-komponent, else 0
                                                                             //                int b3, // 1 if z-direction hat Force-komponent, else 0
                                                                             //                double d1, // Force-komponent in x
                                                                             //                double d2, // Force-komponent in y
                                                                             //                double d3 // Force-komponent in z
                                                                             //                          ]
                                                                             //PS: the definition of xyz seems like not the same as the global XYZ system in SW.
                    swModel.ClearSelection2(true);

                    //meshing
                    CWMesh CWMeshObj = default(CWMesh);
                    CWMeshObj            = Study.Mesh;
                    CWMeshObj.MesherType = (int)swsMesherType_e.swsMesherTypeStandard;
                    CWMeshObj.Quality    = (int)swsMeshQuality_e.swsMeshQualityDraft;
                    errCode   = Study.CreateMesh(0, MeshEleSize, MeshTol);
                    CWMeshObj = null;

                    //run analysis
                    errCode = Study.RunAnalysis();
                    if (errCode != 0)
                    {
                        Console.WriteLine(string.Format("RunAnalysis errCode = {0}", errCode));
                        Console.WriteLine(string.Format("RunAnalysis failed"));



                        swApp.CloseAllDocuments(true);
                        errors = swApp.UnloadAddIn(path_to_cosworks_dll);
                        Console.WriteLine(string.Format("ready to start a new one"));
                        continue;
                    }
                    Console.WriteLine("RunAnalysis successed, ready to get results");

                    //get results
                    CWFeatobj = Study.Results;
                    //get max von Mieses stress
                    Stress = (object[])CWFeatobj.GetMinMaxStress((int)swsStressComponent_e.swsStressComponentVON,
                                                                 0, 0, null,
                                                                 (int)swsStrengthUnit_e.swsStrengthUnitNewtonPerSquareMillimeter,
                                                                 out errCode);
                    maxStress = (float)Stress[3]; //Stress: {node_with_minimum_stress, minimum_stress, node_with_maximum_stress, maximum_stress}
                    Console.WriteLine(maxStress);

                    /*
                     * if (maxStress >= 351.6)
                     * {
                     *  Console.WriteLine("out of yield stress, start a new example");
                     *  errors = swApp.UnloadAddIn(path_to_cosworks_dll);
                     *  swApp.CloseAllDocuments(true);
                     *  j--;
                     *  continue;
                     * }
                     */
                    //get max URES displacement
                    Disp = (object[])CWFeatobj.GetMinMaxDisplacement((int)swsDisplacementComponent_e.swsDisplacementComponentURES,
                                                                     0, null,
                                                                     (int)swsLinearUnit_e.swsLinearUnitMillimeters,
                                                                     out errCode);
                    maxDisp   = (float)Disp[3]; //Disp: {node_with_minimum_displacement, minimum_displacement, node_with_maximum_displacement, maximum_displacement}
                    CWFeatobj = null;
                    Console.WriteLine(string.Format("max Displacement: {0:f4} mm", maxDisp));


                    //output to Excel
                    exlSheet.Cells[j + 2, 1]  = m;
                    exlSheet.Cells[j + 2, 2]  = width * 1000;
                    exlSheet.Cells[j + 2, 3]  = thickness * 1000;
                    exlSheet.Cells[j + 2, 4]  = l1 * 1000;
                    exlSheet.Cells[j + 2, 5]  = l2 * 1000;
                    exlSheet.Cells[j + 2, 6]  = l3 * 1000;
                    exlSheet.Cells[j + 2, 7]  = F1;
                    exlSheet.Cells[j + 2, 8]  = F2;
                    exlSheet.Cells[j + 2, 9]  = F3;
                    exlSheet.Cells[j + 2, 10] = maxStress;
                    exlSheet.Cells[j + 2, 11] = maxDisp;
                    if (j % 5 == 0)
                    {
                        exlBook.Save();//C:\Users\Zhao\Documents\工作簿1.xlsx
                    }

                    errors = swApp.UnloadAddIn(path_to_cosworks_dll);
                    swApp.CloseAllDocuments(true);
                }
                #endregion
            }
            exlBook.SaveCopyAs("D:\\TUD\\7.Semeter\\SA\\SA_code\\c#\\W_Form_analyse_get_TrainingDaten\\W_Form_simulationDaten_" + GetTimeStamp() + "_F3=0" + ".xlsx");
            exlBook.Save();//C:\Users\zhaojie\Documents\工作簿1.xlsx
            exlApp.Quit();
        }
Example #10
0
 public void DoFunc(int rowindex, int sampleindex)
 {
     #region 1.Solidworks应用程序对象--初识SldWorks对象
     if (rowindex == 0)        //
     {
         if (sampleindex == 1) //新建
         {
             SldWorks swApp = API_Learn.Learn_Sldworks.NewSolidworksApp();
             swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
         }
         else if (sampleindex == 2)
         {
             SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
             if (swApp != null)
             {
                 swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
             }
             else
             {
                 MessageBox.Show("无打开的Solidworks");
             }
         }
     }
     #endregion
     #region 2.Solidworks应用程序对象--文档操作1
     else if (rowindex == 1)//
     {
         SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
         if (sampleindex == 1)//新建
         {
             API_Learn.Learn_Sldworks.NewDoc(swApp);
         }
         else if (sampleindex == 2)//打开
         {
             API_Learn.Learn_Sldworks.OpenDoc(swApp);
         }
         else if (sampleindex == 3)//所有文档
         {
             API_Learn.Learn_Sldworks.GetAllOpenedDoc(swApp);
         }
     }
     #endregion
     #region 3.Solidworks应用程序对象--文档操作2
     else if (rowindex == 2)//
     {
         SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
         if (sampleindex == 1)//文档切换
         {
             API_Learn.Learn_Sldworks.ActivateDoc(swApp);
         }
         else if (sampleindex == 2)//加载
         {
             API_Learn.Learn_Sldworks.LoadThirdPartFile(swApp);
         }
         else if (sampleindex == 3)//关闭
         {
             API_Learn.Learn_Sldworks.CloseDoc(swApp);
         }
     }
     #endregion
     #region 4.Solidworks应用程序对象--文档与系统设置
     else if (rowindex == 3)//
     {
         SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
         if (sampleindex == 1)//系统设置
         {
             API_Learn.Learn_Sldworks.SystamSet(swApp);
         }
         else if (sampleindex == 2)//文档设置
         {
         }
         else if (sampleindex == 3)//
         {
         }
     }
     #endregion
 }
Example #11
0
        /// <summary>
        /// Создание головки
        /// </summary>
        public static void CreateHead(double headLength, SldWorks swApp, ModelDoc2 swModel, List <string> detailNames, string pathName)
        {
            var cutDistance = 0.003;
            var dAng        = 0.001;
            var dAng2       = 0.35;
            var dAng3       = 0.02;

            string modelName = pathName + "Головка.sldprt";

            swApp.NewPart();
            swModel = swApp.IActiveDoc2;

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, cutDistance);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, headLength / 3, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, headLength / 3, 0, 0, 0, 0);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, _distanceExtrusion);

            swModel.FeatureManager.FeatureCut3(true, true, true, 0, 0, cutDistance, cutDistance, true, false, false, false, dAng2, dAng2, false, false, false, false, false, true, true, true, true, false, 0, 0, false);

            swModel.Extension.SelectByID2("Плоскость4", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, cutDistance);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, headLength / 6, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, headLength / 2, 0, 0, 0, 0);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Плоскость5", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, _distanceExtrusion);
            swModel.FeatureManager.FeatureCut3(true, true, false, 0, 0, cutDistance, cutDistance, true, false, false, false, dAng2, dAng2, false, false, false, false, false, true, true, true, true, false, 0, 0, false);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Плоскость5", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, _distanceExtrusion);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, headLength / 2, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);

            // Вырез по центру
            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, 0.001);
            swModel.FeatureManager.FeatureCut3(true, false, true, 0, 0, 0.06, 0.06, false, false, false, false, dAng3, dAng3, false, false, false, false, false, true, true, true, true, false, 0, 0, false);

            // Создание гайки для головки
            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, headLength, 0, 0, 0, 0);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Плоскость6", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, 0.004);
            swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.FeatureExtrusion2(true, false, true, 0, 0, dAng, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);
            swModel.ClearSelection();

            swModel.SaveAs(modelName);
            detailNames.Add(modelName);
        }
Example #12
0
        /// <summary>
        /// Создание резьбовой части
        /// </summary>
        ///

        public static void CreateCarving(double carvingLength, SldWorks swApp, ModelDoc2 swModel, List <string> detailNames, double pitchSize, double carvingRadius, double electrodeLength, string pathName)
        {
            var helixRevolution = 12;
            var sidesOfPolygon  = 3;

            string modelName = pathName + "Резьба.sldprt";

            swApp.NewPart();
            swModel = swApp.IActiveDoc2;

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, carvingRadius);
            swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, carvingLength, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);

            // Создание спирали
            swModel.Extension.SelectByID2("", "FACE", 0, 0, carvingLength, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, carvingRadius);
            swModel.InsertHelix(true, true, false, false, 0, carvingLength, pitchSize, helixRevolution, 0, 0);

            // Создание треугольника для выреза резьбы
            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreatePolygon(carvingRadius, -carvingLength - 0.001, 0, carvingRadius - 0.0008, -carvingLength - 0.001, 0, sidesOfPolygon, false);
            swModel.ClearSelection2(true);

            // Выполнение операции "вырезать по направлению"
            swModel.Extension.SelectByID2("Эскиз3", "SKETCH", 0, 0, 0, true, 0, null, 0);
            swModel.Extension.SelectByID2("Спираль1", "REFERENCECURVES", 0, 0, 0, true, 0, null, 0);
            swModel.FeatureManager.InsertCutSwept4(false, true, 0, false, false, 0, 0, false, 0, 0, 0, 0, true, true, 0, true, true, true, false);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, carvingLength, 0, 0, 0, 0);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Плоскость4", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, carvingRadius);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, _distanceExtrusion, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);

            // Вырез под электрод
            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, carvingRadius - _distanceExtrusion);
            swModel.FeatureManager.FeatureCut3(true, false, true, 0, 0, 0.06, 0.06, false, false, false, false, 0.02, 0.02, false, false, false, false, false, true, true, true, true, false, 0, 0, false);

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, 0.002);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, carvingLength + 0.003, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);

            swModel.Extension.SelectByID2("", "FACE", 0, 0, carvingLength + 0.003, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, 0.001);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, electrodeLength, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);

            // Создание второго электрода
            swModel.Extension.SelectByID2("Справа", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, 0.001, 0, 0, 0, 0);

            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, 0, 0, 0, 0, 0);

            swModel.Extension.SelectByID2("Плоскость5", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCornerRectangle(-carvingLength - 0.008, 0.001, 0, -carvingLength - 0.007, -0.001, 0);
            swModel.SketchManager.InsertSketch(true);

            swModel.Extension.SelectByID2("Плоскость6", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.Create3PointArc(-0.0038, -carvingLength - _distanceExtrusion, 0, 0.001, -carvingLength - 0.007, 0, -0.004, -carvingLength - 0.003, 0);
            swModel.SketchManager.InsertSketch(true);

            swModel.Extension.SelectByID2("Эскиз8", "SKETCH", 0, 0, 0, true, 0, null, 0);
            swModel.Extension.SelectByID2("Эскиз9", "SKETCH", 0, 0, 0, true, 0, null, 0);

            swModel.FeatureManager.InsertProtrusionSwept3(false, false, 0, false, false, 0, 0, false, 0, 0, 0, 0, true, true, true, 0, false);

            swModel.SaveAs(modelName);
            detailNames.Add(modelName);
        }
Example #13
0
        /// <summary>
        /// Создание гофрированного цоколя
        /// </summary>
        public static void CreatePlinth(double plinthLength, SldWorks swApp, ModelDoc2 swModel, List <string> detailNames, string textEtching, string pathName)
        {
            var angleExtrusion = 2 * Math.PI;
            var polX           = 0.005;
            var textWidth      = 40;
            var textHeight     = 100;

            string modelName = pathName + "Гофра.sldprt";

            swApp.NewPart();
            swModel = swApp.IActiveDoc2;

            // Создание изолятора
            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, polX);
            swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, plinthLength, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCenterLine(0, 0, 0, 0, -plinthLength, 0);
            swModel.SketchManager.InsertSketch(true);

            // Вырез под гофру
            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreatePolygon(polX + 0.002, 0, 0, polX - 0.001, 0, 0, 3, false);
            swModel.Extension.SelectByID2("Line1@Эскиз2", "EXTSKETCHSEGMENT", 0, 0, 0, true, 16, null, 0);
            swModel.FeatureManager.FeatureRevolve2(true, true, false, true, false, false, 0, 0, angleExtrusion, 0, false, false, 0.01, 0.01, 0, 0, 0, true, true, true);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreatePolygon(polX + 0.002, -0.002, 0, polX - 0.001, -_distanceExtrusion, 0, 3, false);
            swModel.Extension.SelectByID2("Line1@Эскиз2", "EXTSKETCHSEGMENT", 0, 0, 0, true, 16, null, 0);
            swModel.FeatureManager.FeatureRevolve2(true, true, false, true, false, false, 0, 0, angleExtrusion, 0, false, false, 0.01, 0.01, 0, 0, 0, true, true, true);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreatePolygon(polX + 0.002, -0.004, 0, polX - 0.001, -0.004, 0, 3, false);
            swModel.Extension.SelectByID2("Line1@Эскиз2", "EXTSKETCHSEGMENT", 0, 0, 0, true, 16, null, 0);
            swModel.FeatureManager.FeatureRevolve2(true, true, false, true, false, false, 0, 0, angleExtrusion, 0, false, false, 0.01, 0.01, 0, 0, 0, true, true, true);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreatePolygon(polX + 0.002, -0.006, 0, polX - 0.001, -0.006, 0, 3, false);
            swModel.Extension.SelectByID2("Line1@Эскиз2", "EXTSKETCHSEGMENT", 0, 0, 0, true, 16, null, 0);
            swModel.FeatureManager.FeatureRevolve2(true, true, false, true, false, false, 0, 0, angleExtrusion, 0, false, false, 0.01, 0.01, 0, 0, 0, true, true, true);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreatePolygon(polX + 0.002, -0.008, 0, polX - 0.001, -0.008, 0, 3, false);
            swModel.Extension.SelectByID2("Line1@Эскиз2", "EXTSKETCHSEGMENT", 0, 0, 0, true, 16, null, 0);
            swModel.FeatureManager.FeatureRevolve2(true, true, false, true, false, false, 0, 0, angleExtrusion, 0, false, false, 0.01, 0.01, 0, 0, 0, true, true, true);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreatePolygon(polX + 0.002, -0.01, 0, polX - 0.001, -0.01, 0, 3, false);
            swModel.Extension.SelectByID2("Line1@Эскиз2", "EXTSKETCHSEGMENT", 0, 0, 0, true, 16, null, 0);
            swModel.FeatureManager.FeatureRevolve2(true, true, false, true, false, false, 0, 0, angleExtrusion, 0, false, false, 0.01, 0.01, 0, 0, 0, true, true, true);
            swModel.ClearSelection2(true);

            // Создание текста

            swModel.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, 0.006, 0, 0, 0, 0);

            swModel.Extension.SelectByID2("Плоскость4", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.InsertSketchText(-0.003, -plinthLength + 0.002, 0, textEtching, 0, 0, 0, textWidth, textHeight);

            swModel.SketchManager.InsertSketch(true);
            swModel.Extension.SelectByID2("Эскиз9", "SKETCH", 0, 0, 0, false, 4, null, 0);
            swModel.Extension.SelectByID2("", "FACE", 0.005, 0.0007, 0.02, true, 1, null, 0);
            swModel.FeatureManager.InsertWrapFeature(1, 0.001, false);
            //swModel.FeatureManager.FeatureCut3(true, false, true, 0, 0, 0.05, 0.05, false, false, false, false, 0.017, 0.017, false, false, false, false, false, true, true, true, true, false, 0, 0, false);

            swModel.SaveAs(modelName);
            detailNames.Add(modelName);
        }
Example #14
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            // preventing empty fields
            if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text) ||
                string.IsNullOrWhiteSpace(textBox3.Text))
            {
                MessageBox.Show("Enter all values", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            double x1 = Convert.ToDouble(textBox1.Text);
            double x2 = Convert.ToDouble(textBox2.Text);
            double x3 = Convert.ToDouble(textBox3.Text);

            // checking detail size condition
            if (x2 >= 42 - 5)
            {
                MessageBox.Show("Width of base can not exceed or equal the diameter of the cylinder",
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (x2 < 10)
            {
                MessageBox.Show("Width of base can not be less than the width of the rib",
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (x1 <= 42)
            {
                MessageBox.Show("Length of base can not be less than or equal " +
                                "to the diameter of the cylinder!",
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (x3 <= 19)
            {
                MessageBox.Show("Height of detail can not be less than or equal " +
                                "to the length of the inner section of the cylinder!",
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // m to mm conversion
            x1 = -x1 / 1000;
            x2 = -x2 / 1000;
            x3 = -x3 / 1000;

            // Opening SW 2016
            Guid   Guid1     = new Guid("F16137AD-8EE8-4D2A-8CAC-DFF5D1F67522");
            object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromCLSID(Guid1));

            swApp         = (SldWorks)processSW;
            swApp.Visible = true;

            // selection of detail modeling mode
            swApp.NewPart();

            ModelDoc2 swDoc      = null;
            bool      boolstatus = false;

            swDoc = ((ModelDoc2)(swApp.ActiveDoc));

            // selecting a sketch from above, creating a circle and extruding it
            boolstatus = swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ClearSelection2(true);
            SketchSegment skSegment = null;

            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0, 0, 0, -0.021, 0, 0)));
            swDoc.ShowNamedView2("*Trimetric", 8);
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            Feature myFeature = null;

            myFeature = ((Feature)(swDoc.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, Abs(x3),
                                                                          0.01, false, false, false, false, 0.017453292519943334, 0.017453292519943334,
                                                                          false, false, false, false, true, true, true, 0, 0, false)));
            swDoc.ISelectionManager.EnableContourSelection = false;

            // selecting a sketch from above, creating a base (rectangle), adding lines of contact with a circle
            boolstatus = swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SetUserPreferenceToggle(((int)(swUserPreferenceToggle_e.swSketchAddConstToRectEntity)),
                                                                 ((int)(swUserPreferenceOption_e.swDetailingNoOptionSpecified)), true);
            boolstatus = swDoc.Extension.SetUserPreferenceToggle(((int)(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType)),
                                                                 ((int)(swUserPreferenceOption_e.swDetailingNoOptionSpecified)), true);
            Array vSkLines = null;

            vSkLines = ((Array)(swDoc.SketchManager.CreateCenterRectangle(0, 0, 0, x1 / 2, x2 / 2, 0)));
            swDoc.ShowNamedView2("*Top", 5);
            swDoc.ClearSelection2(true);
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(x1 / 2, Abs(x2 / 2), 0.000000, 0.000000, 0.021000, 0.000000)));
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(0.000000, 0.021000, 0.000000, Abs(x1 / 2), Abs(x2 / 2), 0.000000)));
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(x1 / 2, x2 / 2, 0.000000, 0.000000, -0.021000, 0.000000)));
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(0.000000, -0.021000, 0.000000, Abs(x1 / 2), x2 / 2, 0.000000)));
            swDoc.ClearSelection2(true);

            // cutting of internal lines for extrusion
            boolstatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", -0.0065662815082737427, 0, x2 / 2, false, 2, null, 0);
            boolstatus = swDoc.SketchManager.SketchTrim(4, 0, 0, 0);
            boolstatus = swDoc.Extension.SelectByID2("Line3", "SKETCHSEGMENT", -0.0046846865546491534, 0, Abs(x2 / 2), false, 2, null, 0);
            boolstatus = swDoc.SketchManager.SketchTrim(4, 0, 0, 0);
            swDoc.ClearSelection2(true);

            // extruding base
            myFeature = ((Feature)(swDoc.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 0.01, Abs(x3),
                                                                          false, false, false, false, 0.017453292519943334, 0.017453292519943334,
                                                                          false, false, false, false, true, true, true, 0, 0, false)));
            swDoc.ISelectionManager.EnableContourSelection = false;

            // selecting a sketch from above, creating an edge (rectangle) and extruding it
            boolstatus = swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SetUserPreferenceToggle(((int)(swUserPreferenceToggle_e.swSketchAddConstToRectEntity)),
                                                                 ((int)(swUserPreferenceOption_e.swDetailingNoOptionSpecified)), true);
            boolstatus = swDoc.Extension.SetUserPreferenceToggle(((int)(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType)),
                                                                 ((int)(swUserPreferenceOption_e.swDetailingNoOptionSpecified)), true);
            vSkLines = ((Array)(swDoc.SketchManager.CreateCenterRectangle(0, 0, 0, x1 / 2, -0.005, 0)));
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SelectByID2("Line5", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            boolstatus = swDoc.Extension.SelectByID2("Line6", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            boolstatus = swDoc.Extension.SelectByID2("Point1", "SKETCHPOINT", 0, 0, 0, true, 0, null, 0);
            boolstatus = swDoc.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            boolstatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            boolstatus = swDoc.Extension.SelectByID2("Line4", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            boolstatus = swDoc.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);
            myFeature  = ((Feature)(swDoc.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, Abs(x3), 0.01,
                                                                           false, false, false, false, 0.017453292519943334, 0.017453292519943334,
                                                                           false, false, false, false, true, true, true, 0, 0, false)));
            swDoc.ISelectionManager.EnableContourSelection = false;

            // approximation
            swDoc.ViewZoomtofit2();
            swDoc.ViewZoomtofit2();
            swDoc.ViewZoomtofit2();

            // selection of a frontal sketch, creation of lines for diagonal cutting of an edge in height
            boolstatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ClearSelection2(true);
            skSegment = null;
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(x1 / 2, 0.010000, 0.000000, -0.020396, Abs(x3), 0.000000)));
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            myFeature  = ((Feature)(swDoc.FeatureManager.FeatureCut3(false, false, false, 1, 1, +x3 - 10, +x3 - 10,
                                                                     false, false, false, false, 0.017453292519943334, 0.017453292519943334,
                                                                     false, false, false, false, false, true, true, true, true, false, 0, 0, false)));
            swDoc.ISelectionManager.EnableContourSelection = false;

            // selection of frontal view, creation of the second line for diagonal cutting of the edge in height
            boolstatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ClearSelection2(true);
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(Abs(x1 / 2), 0.010000, 0.000000, 0.020396, Abs(x3), 0.000000)));
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            myFeature  = ((Feature)(swDoc.FeatureManager.FeatureCut3(false, true, false, 1, 1, Abs(x3) - 10, Abs(x3) - 10,
                                                                     false, false, false, false, 0.017453292519943334, 0.017453292519943334,
                                                                     false, false, false, false, false, true, true, true, true, false, 0, 0, false)));
            swDoc.ISelectionManager.EnableContourSelection = false;

            // selecting top view, sketch top, creating a circle for the inner notch of the cylinder
            swDoc.ShowNamedView2("*Top", 5);
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ClearSelection2(true);
            swDoc.ClearSelection(false)
            skSegment = null;
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0.000000, 0.000000, 0.000000, -0.016, 0.000000, 0.000000)));
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            myFeature  = null;
            myFeature  = ((Feature)(swDoc.FeatureManager.FeatureCut3(true, false, true, 1, 0, 0.01, 0.01,
                                                                     false, false, false, false, 0.017453292519943334, 0.017453292519943334,
                                                                     false, false, false, false, false, true, true, true, true, false, 0, 0, false)));
            swDoc.ISelectionManager.EnableContourSelection = false;

            // filling the circle with the same radius for the cutout 17 mm below
            boolstatus = swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ClearSelection2(true);
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0.000000, 0.000000, 0.000000, -0.016, 0.000000, 0.000000)));
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            myFeature  = ((Feature)(swDoc.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 0.017000000000000001,
                                                                           0.01, false, false, false, false, 0.017453292519943334, 0.017453292519943334,
                                                                           false, false, false, false, true, true, true, 0, 0, false)));
            swDoc.ISelectionManager.EnableContourSelection = false;

            // creating a circle of smaller radius for another cutout
            boolstatus = swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ClearSelection2(true);
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0.000000, 0.000000, 0.000000, -0.009, 0.000000, 0.000000)));
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            myFeature  = ((Feature)(swDoc.FeatureManager.FeatureCut3(true, false, true, 1, 0, 0.017000000000000001,
                                                                     0.017000000000000001, false, false, false, false, 0.017453292519943334, 0.017453292519943334,
                                                                     false, false, false, false, false, true, true, true, true, false, 0, 0, false)));
            swDoc.ISelectionManager.EnableContourSelection = false;
        }
Example #15
0
 public void DoFunc(int rowindex, int sampleindex)
 {
     #region 1.Solidworks应用程序对象--初识SldWorks对象
     if (rowindex == 0)        //
     {
         if (sampleindex == 1) //新建
         {
             SldWorks swApp = API_Learn.Learn_Sldworks.NewSolidworksApp();
             swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
         }
         else if (sampleindex == 2)
         {
             SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
             if (swApp != null)
             {
                 swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
             }
             else
             {
                 MessageBox.Show("无打开的Solidworks");
             }
         }
     }
     #endregion
     #region 2.Solidworks应用程序对象--文档操作1
     else if (rowindex == 1)//
     {
         SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
         if (sampleindex == 1)//新建
         {
             API_Learn.Learn_Sldworks.NewDoc(swApp);
         }
         else if (sampleindex == 2)//打开
         {
             API_Learn.Learn_Sldworks.OpenDoc(swApp);
         }
         else if (sampleindex == 3)//所有文档
         {
             API_Learn.Learn_Sldworks.GetAllOpenedDoc(swApp);
         }
     }
     #endregion
     #region 3.Solidworks应用程序对象--文档操作2
     else if (rowindex == 2)//
     {
         SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
         if (sampleindex == 1)//文档切换
         {
             API_Learn.Learn_Sldworks.ActivateDoc(swApp);
         }
         else if (sampleindex == 2)//加载
         {
             API_Learn.Learn_Sldworks.LoadThirdPartFile(swApp);
         }
         else if (sampleindex == 3)//关闭
         {
             API_Learn.Learn_Sldworks.CloseDoc(swApp);
         }
     }
     #endregion
     #region 4.Solidworks应用程序对象--文档与系统设置
     else if (rowindex == 3)//
     {
         SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
         if (sampleindex == 1)//系统设置
         {
             API_Learn.Learn_Sldworks.SystamSet(swApp);
         }
         else if (sampleindex == 2)//文档设置
         {
         }
         else if (sampleindex == 3)//
         {
         }
     }
     #endregion
     #region 5.Solidworks文档相关对象简介
     else if (rowindex == 4)//
     {
         SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
         if (sampleindex == 1)//获取文档
         {
             API_Learn.Learn_ModelDoc2.GetDocObject(swApp);
         }
         else if (sampleindex == 2)//
         {
         }
         else if (sampleindex == 3)//
         {
         }
     }
     #endregion
     #region 6.Solidworks草图绘制基础1
     else if (rowindex == 5)   //
     {
         if (sampleindex == 1) //草图绘制
         {
             SldWorks  swApp     = API_Learn.Learn_Sldworks.NewSolidworksApp();
             ModelDoc2 SketchDoc = swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
             API_Learn.Learn_SketchManager.DrawSketch(SketchDoc);
         }
         else if (sampleindex == 2)//
         {
         }
         else if (sampleindex == 3)//
         {
         }
     }
     #endregion
     #region 7.Solidworks草图绘制基础2
     else if (rowindex == 6)   //
     {
         if (sampleindex == 1) //添加集合关系与尺寸
         {
             SldWorks  swApp     = API_Learn.Learn_Sldworks.NewSolidworksApp();
             ModelDoc2 SketchDoc = swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
             try
             {
                 swApp.SetUserPreferenceToggle(10, false);
                 API_Learn.Learn_SketchManager.AddConstraintAndDim(SketchDoc);
             }
             catch
             {
             }
             finally
             {
                 swApp.SetUserPreferenceToggle(10, true);
             }
         }
         else if (sampleindex == 2)//
         {
         }
         else if (sampleindex == 3)//
         {
         }
     }
     #endregion
 }
Example #16
0
 /// <summary>
 /// Метод, создающий файл в котором будет рисоваться деталь.
 /// </summary>
 public void CreateSolidWorksFile()
 {
     _solidWorks.NewPart();
     _model = _solidWorks.IActiveDoc2;
 }
Example #17
0
        private void Button1_Click(object sender, EventArgs e)
        {
            swApp         = new SldWorks();
            swApp.Visible = true;
            swApp.NewPart();
            swModel = swApp.IActiveDoc2;

            swModel.Extension.SelectByID2("Справа", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.InsertSketch(true);

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(-0, 0.004, 0, -0, 0.006, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(-0, 0.006, 0, 0.01, 0.006, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.01, 0.006, 0, 0.01, 0.011, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.01, 0.011, 0, 0.015, 0.011, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.015, 0.011, 0, 0.015, 0.0065, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.015, 0.0065, 0, 0.02, 0.0065, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.02, 0.0065, 0, 0.026, 0.005, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.026, 0.005, 0, 0.028, 0.0065, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.028, 0.0065, 0, 0.034, 0.005, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.034, 0.005, 0, 0.036, 0.0065, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.036, 0.0065, 0, 0.042, 0.005, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.042, 0.005, 0, 0.044, 0.0065, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.044, 0.0065, 0, 0.05, 0.005, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.05, 0.005, 0, 0.05, 0.004, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.05, 0.004, 0, 0, 0.004, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(-0, 0.004, 0, 0.05, 0.004, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateCenterLine(-0, 0, 0, 0.05, 0, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();
            swModel.SketchManager.InsertSketch(true);

            // swModel.Extension.SelectByID2("Line17@Эскиз1", "EXTSKETCHSEGMENT", 0, 9.94238977252735E-02, 0.105960140100876, false, 16, null, 0);
            // swModel.Extension.SelectByID2("Эскиз1", "SKETCH", -2.66009057248503E-03, 3.07631837781458E-03, -1.74442594135417E-02, true, 2, null, 0);
            swModel.SelectionManager.EnableContourSelection = true;
            swModel.Extension.SelectByID2("Эскиз1", "SKETCHREGION", -2.66009057248503E-03, 3.07631837781458E-03, -1.74442594135417E-02, true, 2, null, 0);

            swModel.FeatureManager.FeatureRevolve2(true, true, false, false, false, false, 0, 0, 6.2831853071796, 0, false, false, 0.01, 0.01, 0, 0, 0, true, true, true);
            swModel.SelectionManager.EnableContourSelection = false;


            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.InsertSketch(true);

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateCircle(0, 0, 0, -0.012, -0, 0);

            swModel.SketchManager.CreatePolygon(0, 0, 0, -0.011, 0, 0, 6, true);
            // swModel.SketchAddConstraints("sgFIXED");
            // swModel.SetPickMode();
            swModel.ClearSelection2(true);
            swModel.SketchManager.InsertSketch(true);


            swModel.FeatureManager.FeatureCut4(true, false, false, 0, 0, 0.1, 0.01, false, false, false, false, 1.74532925199433E-02, 1.74532925199433E-02, false, false, false, false, false, true, true, true, true, false, 0, 0, false, false);
            // swModel.SelectionManager.EnableContourSelection = false;



            swModel.Extension.SelectByID2("Справа", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.InsertSketch(true);

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.014, 0.011, 0, 0.015, 0.011, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.015, 0.011, 0, 0.015, 0.009526, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.ClearSelection2(true);
            swModel.SketchManager.CreateLine(0.015, 0.009526, 0, 0.014, 0.011, 0);
            swModel.SketchAddConstraints("sgFIXED");
            swModel.SetPickMode();

            swModel.SketchManager.InsertSketch(true);


            swModel.Extension.SelectByID2("Line17@Эскиз1", "EXTSKETCHSEGMENT", 1.91276838961301E-02, 0, 0, true, 0, null, 0);
            swModel.Extension.SelectByID2("Эскиз3", "SKETCHREGION", 3.39317975691915E-03, 1.13603218378812E-02, -1.59961262883416E-02, true, 0, null, 0);
            swModel.ClearSelection2(true);
            swModel.Extension.SelectByID2("Line17@Эскиз1", "EXTSKETCHSEGMENT", 1.91276838961301E-02, 0, 0, false, 16, null, 0);
            swModel.Extension.SelectByID2("Эскиз3", "SKETCH", 3.39317975691915E-03, 1.13603218378812E-02, -1.59961262883416E-02, true, 2, null, 0);
            swModel.SelectionManager.EnableContourSelection = true;
            swModel.Extension.SelectByID2("Эскиз3", "SKETCHREGION", 3.39317975691915E-03, 1.13603218378812E-02, -1.59961262883416E-02, true, 2, null, 0);

            swModel.FeatureManager.FeatureRevolve2(true, true, false, true, false, false, 0, 0, 6.2831853071796, 0, false, false, 0.01, 0.01, 0, 0, 0, true, true, true);
            swModel.SelectionManager.EnableContourSelection = false;
        }
Example #18
0
        public void DoFunc(int rowindex, int sampleindex)
        {
            #region 1.Solidworks应用程序对象--初识SldWorks对象
            if (rowindex == 0)        //
            {
                if (sampleindex == 1) //新建
                {
                    SldWorks swApp = API_Learn.Learn_Sldworks.NewSolidworksApp();
                    swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
                }
                else if (sampleindex == 2)
                {
                    SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
                    if (swApp != null)
                    {
                        swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
                    }
                    else
                    {
                        MessageBox.Show("无打开的Solidworks");
                    }
                }
            }
            #endregion
            #region 2.Solidworks应用程序对象--文档操作1
            else if (rowindex == 1)//
            {
                SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
                if (sampleindex == 1)//新建
                {
                    API_Learn.Learn_Sldworks.NewDoc(swApp);
                }
                else if (sampleindex == 2)//打开
                {
                    API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"RectanglePlug\PlugTopBox.SLDPRT", true);
                }
                else if (sampleindex == 3)//所有文档
                {
                    API_Learn.Learn_Sldworks.GetAllOpenedDoc(swApp);
                }
            }
            #endregion
            #region 3.Solidworks应用程序对象--文档操作2
            else if (rowindex == 2)//
            {
                SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
                if (sampleindex == 1)//文档切换
                {
                    API_Learn.Learn_Sldworks.ActivateDoc(swApp);
                }
                else if (sampleindex == 2)//加载
                {
                    API_Learn.Learn_Sldworks.LoadThirdPartFile(swApp);
                }
                else if (sampleindex == 3)//关闭
                {
                    API_Learn.Learn_Sldworks.CloseDoc(swApp);
                }
            }
            #endregion
            #region 4.Solidworks应用程序对象--文档与系统设置
            else if (rowindex == 3)//
            {
                SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
                if (sampleindex == 1)//系统设置
                {
                    API_Learn.Learn_Sldworks.SystamSet(swApp);
                }
                else if (sampleindex == 2)//文档设置
                {
                }
                else if (sampleindex == 3)//
                {
                }
            }
            #endregion
            #region 5.Solidworks文档相关对象简介
            else if (rowindex == 4)//
            {
                SldWorks swApp = API_Learn.Learn_Sldworks.GetSolidworksApp();
                if (sampleindex == 1)//获取文档
                {
                    API_Learn.Learn_ModelDoc2.GetDocObject(swApp);
                }
                else if (sampleindex == 2)//
                {
                }
                else if (sampleindex == 3)//
                {
                }
            }
            #endregion
            #region 6.Solidworks草图绘制基础1
            else if (rowindex == 5)   //
            {
                if (sampleindex == 1) //草图绘制
                {
                    SldWorks  swApp     = API_Learn.Learn_Sldworks.NewSolidworksApp();
                    ModelDoc2 SketchDoc = swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
                    API_Learn.Learn_SketchManager.DrawSketch(SketchDoc);
                }
                else if (sampleindex == 2)//
                {
                }
                else if (sampleindex == 3)//
                {
                }
            }
            #endregion
            #region 7.Solidworks草图绘制基础2
            else if (rowindex == 6)   //
            {
                if (sampleindex == 1) //添加集合关系与尺寸
                {
                    SldWorks  swApp     = API_Learn.Learn_Sldworks.NewSolidworksApp();
                    ModelDoc2 SketchDoc = swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
                    try
                    {
                        swApp.SetUserPreferenceToggle(10, false);//不弹出尺寸标注对话框
                        API_Learn.Learn_SketchManager.AddConstraintAndDim(SketchDoc);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        swApp.SetUserPreferenceToggle(10, true);//恢复弹出尺寸标注对话框
                    }
                }
                else if (sampleindex == 2)//
                {
                }
                else if (sampleindex == 3)//
                {
                }
            }
            #endregion
            #region 8.Solidworks特征创建基础1
            else if (rowindex == 7)   //
            {
                if (sampleindex == 1) //拉伸特征
                {
                    SldWorks  swApp   = API_Learn.Learn_Sldworks.NewSolidworksApp();
                    ModelDoc2 FeatDoc = swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
                    try
                    {
                        swApp.SetUserPreferenceToggle(10, false);//不弹出尺寸标注对话框
                        API_Learn.Learn_SketchManager.AddConstraintAndDim(FeatDoc);
                        FeatDoc.ClearSelection2(true);
                        API_Learn.Learn_FeatureManager.CreateFeature1(FeatDoc);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        swApp.SetUserPreferenceToggle(10, true);//恢复弹出尺寸标注对话框
                    }
                }
                else if (sampleindex == 2)//基准
                {
                    SldWorks  swApp   = API_Learn.Learn_Sldworks.NewSolidworksApp();
                    ModelDoc2 FeatDoc = swApp.NewPart();//新建零件,验证获得Solidworks程序对象成功
                    try
                    {
                        API_Learn.Learn_FeatureManager.CreatePlane(FeatDoc);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        swApp.SetUserPreferenceToggle(10, true);//恢复弹出尺寸标注对话框
                    }
                }
                else if (sampleindex == 3)//
                {
                }
            }
            #endregion
            #region 9.Solidworks属性
            else if (rowindex == 8)//
            {
                SldWorks  swApp  = API_Learn.Learn_Sldworks.GetSolidworksApp();
                ModelDoc2 Doc    = swApp.ActiveDoc;
                bool      ToOpen = false;
                if (Doc == null)
                {
                    ToOpen = true;
                }
                else
                {
                    if (Doc.GetTitle() != "Solidworks属性学习.SLDPRT")
                    {
                        ToOpen = true;
                    }
                }
                if (ToOpen)
                {
                    Doc = API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"Sample\9\Solidworks属性学习.SLDPRT", false);
                }

                if (Doc != null)
                {
                    if (sampleindex == 1)//属性写入
                    {
                        API_Learn.Learn_CustomPropertyManager.WriteCusp(Doc);
                    }
                    else if (sampleindex == 2)//属性读取
                    {
                        API_Learn.Learn_CustomPropertyManager.ReadCusp(Doc);
                    }
                }
            }
            #endregion
            #region 9.Solidworks配置
            else if (rowindex == 9)//
            {
                SldWorks  swApp  = API_Learn.Learn_Sldworks.GetSolidworksApp();
                ModelDoc2 Doc    = swApp.ActiveDoc;
                bool      ToOpen = false;
                if (Doc == null)
                {
                    ToOpen = true;
                }
                else
                {
                    if (Doc.GetTitle() != "ConfigPart.SLDPRT")
                    {
                        ToOpen = true;
                    }
                }
                if (ToOpen)
                {
                    Doc = API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"Sample\10\ConfigPart.SLDPRT", false);
                }

                if (Doc != null)
                {
                    if (sampleindex == 1)//创建配置
                    {
                        API_Learn.Learn_Configuration.CreateConfig(Doc);
                    }
                    else if (sampleindex == 2)//读取配置
                    {
                        API_Learn.Learn_Configuration.GetConfig(Doc);
                    }
                    else if (sampleindex == 3)//删除配置
                    {
                        API_Learn.Learn_Configuration.DelConfig(Doc);
                    }
                }
            }
            #endregion
            #region 10.获取选择对象
            else if (rowindex == 10)//
            {
                SldWorks  swApp  = API_Learn.Learn_Sldworks.GetSolidworksApp();
                ModelDoc2 Doc    = swApp.ActiveDoc;
                bool      ToOpen = false;
                if (Doc == null)
                {
                    ToOpen = true;
                }
                else
                {
                    if (Doc.GetTitle() != "SelectionMgr.SLDPRT")
                    {
                        ToOpen = true;
                    }
                }
                if (ToOpen)
                {
                    Doc = API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"Sample\11\SelectionMgr.SLDPRT", false);
                }

                if (Doc != null)
                {
                    if (sampleindex == 1)//获取面
                    {
                        API_Learn.Learn_SelectionMgr.GetFace(Doc);
                    }
                    else if (sampleindex == 2)//获取边线
                    {
                        API_Learn.Learn_SelectionMgr.GetEdge(Doc);
                    }
                    else if (sampleindex == 3)//
                    {
                        API_Learn.Learn_SelectionMgr.GetSelectList(Doc);
                    }
                }
            }
            #endregion
            #region 11.Solidworks测量工具
            else if (rowindex == 11)//
            {
                SldWorks  swApp  = API_Learn.Learn_Sldworks.GetSolidworksApp();
                ModelDoc2 Doc    = swApp.ActiveDoc;
                bool      ToOpen = false;
                if (Doc == null)
                {
                    ToOpen = true;
                }
                else
                {
                    if (Doc.GetTitle() != "Measure.SLDPRT")
                    {
                        ToOpen = true;
                    }
                }
                if (ToOpen)
                {
                    Doc = API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"Sample\12\Measure.SLDPRT", false);
                }

                if (Doc != null)
                {
                    if (sampleindex == 1)//圆柱
                    {
                        API_Learn.Learn_Measure.MeasureFace(Doc);
                    }
                    else if (sampleindex == 2)//边线
                    {
                        API_Learn.Learn_Measure.MeasureEdge(Doc);
                    }
                    else if (sampleindex == 3)//圆柱与点
                    {
                        API_Learn.Learn_Measure.MeasurePointFace(Doc);
                    }
                }
            }
            #endregion
            #region 12.Solidworks尺寸
            else if (rowindex == 12)//
            {
                SldWorks  swApp  = API_Learn.Learn_Sldworks.GetSolidworksApp();
                ModelDoc2 Doc    = swApp.ActiveDoc;
                bool      ToOpen = false;
                if (Doc == null)
                {
                    ToOpen = true;
                }
                else
                {
                    if (Doc.GetTitle() != "Measure.SLDPRT")
                    {
                        ToOpen = true;
                    }
                }
                if (ToOpen)
                {
                    Doc = API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"Sample\13\Dimension.SLDPRT", false);
                }

                if (Doc != null)
                {
                    if (sampleindex == 1)//圆柱
                    {
                        API_Learn.Learn_Dimension.GetDim(Doc, "L@草图1");
                    }
                    else if (sampleindex == 2)//边线
                    {
                        Dictionary <string, double> Vals = new Dictionary <string, double>();
                        Vals.Add("Default", 66);
                        Vals.Add("cfg2", 77);
                        API_Learn.Learn_Dimension.SetDim(Doc, "L@草图1", Vals);
                    }
                    else if (sampleindex == 3)//圆柱与点
                    {
                    }
                }
            }
            #endregion
            #region 13.Solidworks装配体对象
            else if (rowindex == 13)//
            {
                SldWorks  swApp  = API_Learn.Learn_Sldworks.GetSolidworksApp();
                ModelDoc2 Doc    = swApp.ActiveDoc;
                bool      ToOpen = false;
                if (Doc == null)
                {
                    ToOpen = true;
                }
                else
                {
                    if (Doc.GetTitle() != "Assem.SLDASM")
                    {
                        ToOpen = true;
                    }
                }
                if (ToOpen)
                {
                    Doc = API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"Sample\14\Assem.SLDASM", false, swDocumentTypes_e.swDocASSEMBLY);
                }

                if (Doc != null)
                {
                    if (sampleindex == 1)//获取子部件
                    {
                        API_Learn.Learn_AssemblyDoc.GetChildrenComp(Doc);
                    }
                    else if (sampleindex == 2)//插入零部件
                    {
                        API_Learn.Learn_AssemblyDoc.InsertPart(swApp, Doc, AppDomain.CurrentDomain.BaseDirectory + @"Sample\14\part3.SLDPRT");
                    }
                    else if (sampleindex == 3)//
                    {
                    }
                }
            }
            #endregion
            #region 14.Solidworks装配体对象
            else if (rowindex == 14)//
            {
                SldWorks  swApp  = API_Learn.Learn_Sldworks.GetSolidworksApp();
                ModelDoc2 Doc    = swApp.ActiveDoc;
                bool      ToOpen = false;
                if (Doc == null)
                {
                    ToOpen = true;
                }
                else
                {
                    if (Doc.GetTitle() != "装配学习.SLDASM")
                    {
                        ToOpen = true;
                    }
                }
                if (ToOpen)
                {
                    Doc = API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"Sample\15\装配学习.SLDASM", false, swDocumentTypes_e.swDocASSEMBLY);
                }

                if (Doc != null)
                {
                    if (sampleindex == 1)//部件装配
                    {
                        API_Learn.Learn_AssemblyDoc.AddMate(swApp, Doc, AppDomain.CurrentDomain.BaseDirectory + @"Sample\15\转轴.SLDPRT");
                    }
                    else if (sampleindex == 2)//
                    {
                    }
                    else if (sampleindex == 3)//
                    {
                    }
                }
            }
            #endregion
            #region 15.Solidworks装配体部件设置
            else if (rowindex == 15)//
            {
                SldWorks  swApp  = API_Learn.Learn_Sldworks.GetSolidworksApp();
                ModelDoc2 Doc    = swApp.ActiveDoc;
                bool      ToOpen = false;
                if (Doc == null)
                {
                    ToOpen = true;
                }
                else
                {
                    if (Doc.GetTitle() != "装配学习.SLDASM")
                    {
                        ToOpen = true;
                    }
                }
                if (ToOpen)
                {
                    Doc = API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"Sample\16\装配学习.SLDASM", false, swDocumentTypes_e.swDocASSEMBLY);
                }

                if (Doc != null)
                {
                    Component2 SwComp = ((AssemblyDoc)swApp.ActiveDoc).GetComponentByName("转轴-1");
                    if (sampleindex == 1)//部件状态设置,压缩,解压,轻化
                    {
                        API_Learn.Learn_Component2.SetComponentsStatus(SwComp);
                    }
                    else if (sampleindex == 2)//排除明细表
                    {
                        API_Learn.Learn_Component2.SetCompBomInclude(SwComp);
                    }
                    else if (sampleindex == 3)//配置
                    {
                        API_Learn.Learn_Component2.SetCompConfig(swApp.ActiveDoc, SwComp);
                    }
                }
            }
            #endregion
            #region 16.Solidworks装配体配合对象
            else if (rowindex == 16)//
            {
                SldWorks  swApp  = API_Learn.Learn_Sldworks.GetSolidworksApp();
                ModelDoc2 Doc    = swApp.ActiveDoc;
                bool      ToOpen = false;
                if (Doc == null)
                {
                    ToOpen = true;
                }
                else
                {
                    if (Doc.GetTitle() != "装配学习.SLDASM")
                    {
                        ToOpen = true;
                    }
                }
                if (ToOpen)
                {
                    Doc = API_Learn.Learn_Sldworks.OpenDoc(swApp, AppDomain.CurrentDomain.BaseDirectory + @"Sample\17\装配学习.SLDASM", false, swDocumentTypes_e.swDocASSEMBLY);
                }

                if (Doc != null)
                {
                    Component2 SwComp = ((AssemblyDoc)swApp.ActiveDoc).GetComponentByName("转轴-1");
                    if (sampleindex == 1)//获得配合对象
                    {
                        API_Learn.Learn_Mate2.GetMateType(SwComp);
                    }
                    else if (sampleindex == 2)//获得配合参数
                    {
                        API_Learn.Learn_Mate2.GetMateRefrence(SwComp);
                    }
                    else if (sampleindex == 3)
                    {
                    }
                }
            }
            #endregion
        }
Example #19
0
        /// <summary>
        /// Создание гайки
        /// </summary>
        public static void CreateNut(double nutLength, double nutSize, SldWorks swApp, ModelDoc2 swModel, List <string> detailNames, double chamferRadius, string pathName)
        {
            var dAng2 = 0.45;

            string modelName = pathName + "Болт.sldprt";
            Array  vSkLines  = null;

            swApp.NewPart();
            swModel = swApp.IActiveDoc2;

            // Шайба с фаской 1
            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, chamferRadius);
            swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, _distanceExtrusion, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, _distanceExtrusion, 0, 0, 0, 0);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, 0.006);
            swModel.FeatureManager.FeatureCut3(true, true, true, 0, 0, _distanceExtrusion, _distanceExtrusion, true, false, false, false, 0.30, 0.30, false, false, false, false, false, true, true, true, true, false, 0, 0, false);

            // Гайка
            swModel.Extension.SelectByID2("Плоскость4", "PLANE", 0, 0, 0, false, 0, null, 0);
            vSkLines = swModel.SketchManager.CreatePolygon(0, 0, 0, 0, CandleMathHelper.GetPolygonRadius(nutSize), 0, 6, true);
            double a = CandleMathHelper.GetPolygonRadius(nutSize);

            swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, nutLength - 0.003, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);

            // Фаска гайки 1
            swModel.Extension.SelectByID2("Плоскость4", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, chamferRadius);
            swModel.FeatureManager.FeatureCut3(true, true, true, 0, 0, 0.001, 0.001, true, false, false, false, _dAngle, _dAngle, false, false, false, false, false, true, true, true, true, false, 0, 0, false);

            // Фаска гайки 2
            swModel.Extension.SelectByID2("", "FACE", 0, 0, nutLength, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, chamferRadius);
            swModel.FeatureManager.FeatureCut3(true, true, false, 0, 0, 0.001, 0.001, true, false, false, false, _dAngle, _dAngle, false, false, false, false, false, true, true, true, true, false, 0, 0, false);

            // Шайба с фаской 2
            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, nutLength - 0.001, 0, 0, 0, 0);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Плоскость5", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, chamferRadius);
            swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 0.001, 0, false, false, false, false, _draftAngle, _draftAngle, false, false, false, false, true, true, true, 0, 0, false);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.FeatureManager.InsertRefPlane(_firstConstraint, nutLength, 0, 0, 0, 0);
            swModel.ClearSelection2(true);

            swModel.Extension.SelectByID2("Плоскость6", "PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.SketchManager.CreateCircleByRadius(0, 0, 0, 0.006);
            swModel.FeatureManager.FeatureCut3(true, true, false, 0, 0, 0.001, 0.001, true, false, false, false, dAng2, dAng2, false, false, false, false, false, true, true, true, true, false, 0, 0, false);

            swModel.SaveAs(modelName);
            detailNames.Add(modelName);
        }