2 Replies Latest reply on Aug 3, 2018 8:11 AM by Alex Burnett

    opening solidworks part file with visual studio

    Shubham Misal

      hello sir, i have made a windows form application and added the button with the following program in it. i opened solidworks in another window. but it is giving error. i attached the error snapshot.

      code:

       

      using System;

      using System.Collections.Generic;

      using System.ComponentModel;

      using System.Data;

      using System.Drawing;

      using System.Linq;

      using System.Text;

      using System.Threading.Tasks;

      using System.Windows.Forms;

      using SolidWorks.Interop.sldworks;

      using SolidWorks.Interop.swcommands;

      using SolidWorks.Interop.swconst;

       

      namespace WindowsFormsApplication11

      {

          public partial class Form1 : Form

          {

              public Form1()

              {

                  InitializeComponent();

              }

       

              private void Form1_Load(object sender, EventArgs e)

              {

       

              }

       

              private void button1_Click(object sender, EventArgs e)

              {

                  ModelDoc2 swDoc = null;

                  PartDoc swPart = null;

                  DrawingDoc swDrawing = null;

                  AssemblyDoc swAssembly = null;

                  bool boolstatus = false;

                  int longstatus = 0;

                  int longwarnings = 0;

                  //

                  // New Document

                  double swSheetWidth;

                  swSheetWidth = 0;

                  double swSheetHeight;

                  swSheetHeight = 0;

                  swDoc = ((ModelDoc2)(swApp.NewDocument("C:\\ProgramData\\SolidWorks\\SOLIDWORKS 2017\\templates\\Part.prtdot", 0, swSheetWidth, swSheetHeight)));

                  swPart = (PartDoc)swDoc;

                  swApp.ActivateDoc2("Part1", false, ref longstatus);

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

                  ModelView myModelView = null;

                  myModelView = ((ModelView)(swDoc.ActiveView));

                  myModelView.FrameState = ((int)(swWindowState_e.swWindowMaximized));

                  myModelView = ((ModelView)(swDoc.ActiveView));

                  myModelView.FrameState = ((int)(swWindowState_e.swWindowMaximized));

                  //

                  // Redraw

                  swDoc.GraphicsRedraw2();

                  boolstatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", -0.022361175131038635, 0.12316749145668382, 0.0998444429784513, false, 0, null, 0);

                  swDoc.SketchManager.InsertSketch(true);

                  swDoc.ClearSelection2(true);

                  SketchSegment skSegment = null;

                  skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0, 0, 0, 0.080224, -0.000461, 0)));

                  swDoc.ClearSelection2(true);

                  swDoc.SketchManager.InsertSketch(true);

                  //

                  // Named View

                  swDoc.ShowNamedView2("*Trimetric", 8);

                  swDoc.ViewZoomtofit2();

                  swDoc.SketchManager.InsertSketch(true);

                  Feature myFeature = null;

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

                  swDoc.ISelectionManager.EnableContourSelection = false;

              }

       

              /// <summary>

              ///  The SldWorks swApp variable is pre-assigned for you.

              /// </summary>

              public SldWorks swApp;

       

              }

          }