コード例 #1
0
        private void createPart()
        {
            swDoc = null;
            int longstatus = 0;

            swDoc = ((ModelDoc2)(sldApp.NewDocument("C:\\ProgramData\\SOLIDWORKS\\SOLIDWORKS 2016\\templates\\Part.prtdot", 0, 0, 0)));
            sldApp.ActivateDoc2("Part2", false, ref longstatus);
            swDoc = ((ModelDoc2)(sldApp.ActiveDoc));
            ModelView myModeview = null;

            myModeview            = ((ModelView)(swDoc.ActiveView));
            myModeview.FrameState = ((int)(swWindowState_e.swWindowMaximized));
        }
コード例 #2
0
        private void initSolidworks()
        {
            sldApp = new SldWorks();
            int longstatus = 0;

            sldApp.ActivateDoc2("Part2", false, ref longstatus);
            swDoc = ((ModelDoc2)(sldApp.ActiveDoc));
        }
コード例 #3
0
        public void Main()
        {
            swSuccess = false; errors = 0; warnings = 0;
            string swSourcePath = swApp.GetCurrentWorkingDirectory();
            string swExportPath = Path.Combine(swSourcePath, "IGS");

            string[] tmp = Directory.GetFiles(swSourcePath, "*.SLDPRT", SearchOption.TopDirectoryOnly);

            if (Directory.Exists(swExportPath))
            {
                Directory.Delete(swExportPath, true);
            }

            Directory.CreateDirectory(swExportPath);
            ModelDoc2 swModel = swApp.ActiveDoc as ModelDoc2;

            AssemblyDoc swAssem = (AssemblyDoc)swModel;

            #region TopLevel
            string igsFileName = MakeFileName(swModel.Extension.Document.GetTitle());
            swModel.Extension.SaveAs(Path.Combine(swExportPath, igsFileName), (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Copy, null, ref errors, ref warnings);
            //PrintSaveResults();
            #endregion TopLevel

            #region Subassemblies
            string swExportSubPath = Path.Combine(swExportPath, "Subassemblies");
            Directory.CreateDirectory(swExportSubPath);
            object[]  objComps = (object[])swAssem.GetComponents(true);
            ModelDoc2 igsModel;

            foreach (object obj in objComps)
            {
                swComponent = obj as Component2;
                Debug.WriteLine(String.Format("Working on {0}.. \n\t {1}", swComponent.Name2, swComponent.GetPathName()));
                igsModel = swApp.OpenDoc6(swComponent.GetPathName(), (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_ReadOnly, null, ref errors, ref warnings);
                swApp.ActivateDoc2(igsModel.GetTitle(), true, ref errors);
                igsFileName = MakeFileName(igsModel.Extension.Document.GetTitle());
                igsModel.Extension.SaveAs(Path.Combine(swExportSubPath, igsFileName), (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Copy, null, ref errors, ref warnings);
                swApp.CloseDoc(igsModel.GetTitle());
            }
            #endregion Subassemblies

            #region Parts
            string swExportPartPath = Path.Combine(swExportPath, "Parts");
            Directory.CreateDirectory(swExportPartPath);
            string[] swPartFiles = Directory.GetFiles(swSourcePath, "*.SLDPRT", SearchOption.TopDirectoryOnly);
            foreach (string partFile in swPartFiles)
            {
                try
                {
                    igsModel = swApp.OpenDoc6(partFile, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_ReadOnly, null, ref errors, ref warnings);
                    swApp.ActivateDoc2(igsModel.GetTitle(), true, ref errors);
                    string[] swConfigs     = (string[])igsModel.GetConfigurationNames();
                    int      swConfigCount = igsModel.GetConfigurationCount();
                    string   swConfigFilename;

                    for (int i = 0; i < swConfigCount; i++)
                    {
                        igsModel.ShowConfiguration2(swConfigs[i]);
                        swConfigFilename = igsModel.Extension.Document.GetTitle();
                        if (swConfigCount > 1)
                        {
                            swConfigFilename += String.Format("-CFG{0}", (i + 1).ToString());
                        }
                        igsFileName = MakeFileName(swConfigFilename);
                        igsModel.Extension.SaveAs(Path.Combine(swExportPartPath, igsFileName), (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Copy, null, ref errors, ref warnings);
                    }
                    swApp.CloseDoc(igsModel.GetTitle());
                }
                catch (NullReferenceException e)
                {
                    Debug.WriteLine(e.Message);
                }
            }
            #endregion Parts

            string timeFilename = Path.Combine(swExportPath, "Timestamp.txt");

            using (StreamWriter swTimeStamper = new StreamWriter(timeFilename))
            {
                swTimeStamper.WriteLine("Exported on {0} at {1}", System.DateTime.Now.ToShortDateString(), System.DateTime.Now.ToShortTimeString());
                swTimeStamper.Flush();
            }
        }
コード例 #4
0
        private void OpenAssemblyButtonClick(object sender, EventArgs e)
        {
            // display an open file dialog where user can select an assembly file
            OpenFileDialog openAssemblyFileDialog = new OpenFileDialog
            {
                InitialDirectory = "E:\\Files\\KPI\\macro",
                Filter           = "SolidWorks2014 assembly files|*.sldasm",
                Title            = "Select a SolidWorks Assembly File"
            };

            // show the dialog
            // if the user clicked OK in the dialog and
            // a file was selected, get name of the file
            if (openAssemblyFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // get name of the assembly file
                string fileName = openAssemblyFileDialog.FileName;

                // start solidworks
                swApp = new SldWorks
                {
                    Visible = this.swVisible.Checked
                };

                // open assembly file
                swDoc = OpenAssembly(fileName);

                // activate opened document
                swApp.ActivateDoc2("sborka", false, 0);

                // get active document
                swDoc = (ModelDoc2)swApp.ActiveDoc;

                // get assembly object
                swAsm = (AssemblyDoc)swDoc;

                // get first feature in the doc
                feature = swDoc.FirstFeature();

                List <string> featureNames = new List <string>();

                while (feature != null)
                {
                    featureNames.Add("Feature: " + feature.GetTypeName2());
                    if (feature.GetTypeName2() == "MateGroup")
                    {
                        // get first sub feature in the mate group
                        subFeature = feature.GetFirstSubFeature();

                        // for all sub features get all dimesions
                        while (subFeature != null)
                        {
                            featureNames.Add("-> SubFeature: " + subFeature.GetTypeName2());

                            displayDimension = subFeature.GetFirstDisplayDimension();

                            // for all display dimensions show their dimension
                            while (displayDimension != null)
                            {
                                dimension        = displayDimension.GetDimension2(0);
                                displayDimension = subFeature.GetNextDisplayDimension(displayDimension);
                            }

                            subFeature = subFeature.GetNextSubFeature();
                        }
                    }
                    else
                    {
                        subFeature = feature.GetFirstSubFeature();

                        while (subFeature != null)
                        {
                            featureNames.Add("-> SubFeature: " + subFeature.GetTypeName2());

                            subFeature = subFeature.GetNextSubFeature();
                        }
                    }
                    feature = feature.GetNextFeature();
                }
                this.textBox.AppendText("\n====== Start ======\n");
                this.textBox.AppendText("File path: " + fileName + "\n");
                this.textBox.AppendText(String.Join("\n", featureNames));
                this.textBox.AppendText("\n====== End ======\n");
            }

            swApp.ExitApp();
        }
コード例 #5
0
        //Microsoft.Office.Interop.MSProject


        //string[] args
        static void CreateSketch()
        {
            SldWorks swApp = new SldWorks();
            //Feature myFeature;
            ModelDoc2 swModel = default(ModelDoc2);
            //RefPlane myRefPlane;
            SketchManager  swSkMgr = default(SketchManager);
            FeatureManager swFeMgr = default(FeatureManager);

            //Feature swFeat;
            //Sketch swSketch;
            int  longstatus = 0;
            bool boolstatus = false;

            swApp.ResetUntitledCount(0, 0, 0);

            swModel = (ModelDoc2)swApp.NewDocument("C:\\Users\\nemati\\Documents\\Visual Studio 2015\\Projects\\SWBlankApp\\SWBlankApp\\PartTemplate.SLDPRT", 0, 0, 0);
            swApp.ActivateDoc2("Part1", false, ref longstatus);
            swModel = (ModelDoc2)swApp.ActiveDoc;


            swSkMgr = swModel.SketchManager;
            swFeMgr = swModel.FeatureManager;
            swSkMgr.InsertSketch(true);
            //swFeMgr.FeatureExtruRefSurface()
            boolstatus = swModel.Extension.SelectByID2("Top Plane", "PLANE", -0.0553489443349025, 0.00330468607538553, 0.0269617286188933, false, 0, null, 0);
            swModel.ClearSelection2(true);


            if (swModel == null)
            {
                swApp.SendMsgToUser2("Ridi.", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk);
                return;
            }

            int modelType = 0;

            modelType = swModel.GetType();

            if (modelType != (int)swDocumentTypes_e.swDocPART)
            {
                swApp.SendMsgToUser2("A part document must be active.", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk);
                return;
            }

            // Creating sample point
            SketchPoint skPoint = default(SketchPoint);

            skPoint = swSkMgr.CreatePoint(-100, 100, 0);


            // Creating sample arc
            SketchSegment skArc = default(SketchSegment);

            skArc = swSkMgr.Create3PointArc(0, 0, 0, 10, 10, 0, -10, 10, 0);


            // Creating sample circle
            SketchSegment skCircle = default(SketchSegment);

            skCircle = swSkMgr.CreateCircleByRadius(-20, 20, 0, 7.5);


            swSkMgr.InsertSketch(true);



            //swModel.SketchCircle();

            Console.WriteLine(swApp.Visible.ToString());

            Console.WriteLine(swApp.FrameWidth.ToString());

            //swApp.CreateNewWindow();

            swModel.SaveAs("C:\\Users\\nemati\\Documents\\Visual Studio 2015\\Projects\\SWBlankApp\\SWBlankApp\\SketchArc.SLDPRT");


            //swApp.

            swApp.ExitApp();

            //swApp = null;

            //swApp
        }