2 Replies Latest reply on Dec 6, 2017 9:53 AM by Vniiaen Vniiaen

    the speed of drawing elements in the Solidworks drawing at run time C

    Vniiaen Vniiaen

      Hi,

      I wrote the code in Visual Studio for SolidWorks 2012, which performs the creation of a drawing based on the detail of the part (main views), sections, sections and dimensions. Is it possible to increase the speed of drawing elements during the execution of the code, because now it is not done fast enough? Is there a function that accelerates this process?

       

      Below is the part of the code for drawing the main views, creating the cut and cropping the views.

       

      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;

      using System.IO;

       

      namespace CreateDrawing

      {

          public partial class Form1 : Form

          {

              SldWorks swApp;

          

              public Form1()

              {

                  InitializeComponent();

              }

              //функция проверки открытия файла

              public bool IsFileOpen(string path)

              {

                  if (string.IsNullOrEmpty(path))

                      throw new ArgumentException("'path' cannot be null or empty.", "path");

       

                  try

                  {

                      using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { }

                  }

                  catch (IOException)

                  {

                      return true;

                  }

                  return false;

              }

                     private void button2_Click(object sender, EventArgs e)

              {

                  Close();

              }

              private void button1_Click(object sender, EventArgs e)

              {

                  string User;

                  User = System.Environment.UserName;

                  ModelDoc2 Part;

                  SelectionMgr swSelMgr;

                  SolidWorks.Interop.sldworks.View myView;

                  DrawingDoc swDrawing;

       

                  //запускает SolidWorks2012

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

                  Guid myGuid1 = new Guid("B4875E89-91F6-4124-BB63-2539727E98FA"); //GUID для Solidworks 2012

                  object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromCLSID(myGuid1));

       

                  swApp = (SldWorks)processSW;

                  swApp.Visible = true;

       

                  int longstatus = 0;

                  int longwarnings = 0;

       

                  try

                  {

                      Part = (ModelDoc2)swApp.ActiveDoc;

                      if (IsFileOpen("C:\\IM\\IMWork\\Направляющий аппарат с торовой поверхностью.SLDPRT"))

                      {

                          goto a;

                      }

                      else

                      {

                          Part = (ModelDoc2)swApp.OpenDoc6(@"C:\IM\IMWork\Направляющий аппарат с торовой поверхностью.SLDPRT", 1, 0, "", ref longstatus, ref longwarnings);

                          goto a;

                      };

                      a:

                      bool boolstatus;

                      Object vScaleRatio;

                      string retval;

                  retval = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateDrawing);

                  //Part = swApp.NewDocument(@"C:\ProgramData\SolidWorks\SolidWorks 2012\templates\gost-part drw.drwdot", 0, 0, 0);

                  Part  = (ModelDoc2)swApp.NewDocument(retval, 0, 0, 0);

                  swSelMgr = Part.SelectionManager as SelectionMgr;

                  swDrawing = Part as DrawingDoc;

                  myView = (SolidWorks.Interop.sldworks.View)swDrawing.CreateDrawViewFromModelView3("C:\\IM\\IMWork\\Направляющий аппарат с торовой поверхностью.SLDPRT", "*Слева", 0.13835806060864, 8.53251696525708E-02, 0);

                      boolstatus = Part.Extension.SelectByID2("Чертежный вид1", "DRAWINGVIEW", 0, 0, 0, false, 0, null, 0);

                      boolstatus = swDrawing.ActivateView("Чертежный вид1");

                      vScaleRatio = myView.ScaleRatio;

                      myView.ScaleDecimal = myView.ScaleDecimal * 5;

                      Part.EditRebuild3();

                      myView = swDrawing.CreateUnfoldedViewAt3(-0.210437678570781, 8.53251696525708E-02, 0, false);

                      Part.ClearSelection2(true);

                      boolstatus = Part.Extension.SelectByID2("Чертежный вид1", "DRAWINGVIEW", 0, 0, 0, false, 0, null, 0);

                      myView = swDrawing.CreateUnfoldedViewAt3(0.516477335382791, 8.53251696525708E-02, 0, false);

                      Part.ClearSelection2(true);

                      Part.ViewZoomtofit2();

       

                      object pointArray, skSegment, myCenterLine;

                      double[] points1;

                      double[] points2;

                      double[] points3;

                      double Beta, D2, BetaToRad, L12;

                      const double pi = 3.14159265358979;

                  

                      Beta = 5;//////////////////////////////

                      BetaToRad = Beta * pi / 180;

                      D2 = 420;////////////////////////////

                      L12 = 64;///////////////////////////

       

                      boolstatus = swDrawing.ActivateView("Чертежный вид1");

       

                      skSegment = Part.SketchManager.CreateLine(0 - 0.04, -(((D2) / 2) + 10) / 1000.0, 0, 0 - 0.04, (((D2) / 2) + 10) / 1000.0, 0);

                      skSegment = Part.SketchManager.CreateLine(0 - 0.04, (((D2) / 2) + 10) / 1000.0, 0, ((L12) + 10) / 1000.0, (((D2) / 2) + 10) / 1000.0, 0);

                      skSegment = Part.SketchManager.CreateLine(((L12) + 10) / 1000.0, (((D2) / 2) + 10) / 1000.0, 0, ((L12) + 10) / 1000.0, -(((D2) / 2) + 10) / 1000.0, 0);

                      skSegment = Part.SketchManager.CreateLine(((L12) + 10) / 1000.0, -(((D2) / 2) + 10) / 1000.0, 0, 0 - 0.04, -(((D2) / 2) + 10) / 1000.0, 0);

       

                      boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                      boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                      boolstatus = Part.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                      boolstatus = Part.Extension.SelectByID2("Line4", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                      swDrawing.CreateBreakOutSection(((D2) / 2) / 1000.0);

                      Part.ClearSelection2(true);

       

                      boolstatus = swDrawing.ActivateView("Чертежный вид1");

                      boolstatus = Part.Extension.SelectByID2("", "EDGE", 0.139425110594787, 0.142762852404969, 0, false, 0, null, 0);

                      boolstatus = Part.Extension.SelectByID2("", "EDGE", 0.14214405415703, 2.72077510096118E-02, 0, true, 0, null, 0);

       

                      myCenterLine = swDrawing.InsertCenterLine2();

                      Part.ClearSelection2(true);

                      boolstatus = swDrawing.ActivateView("Чертежный вид2");

       

                      points1 = new double[33];

                      points1[0] = -0.214318;

                      points1[1] = -4.52024367223014E-02;

                      points1[2] = 0;

                      points1[3] = -0.117115767649788;

                      points1[4] = -2.82090394582783E-02;

                      points1[5] = 0;

                      points1[6] = 5.91642854173952E-03;

                      points1[7] = -3.29671906922048E-02;

                      points1[8] = 0;

                      points1[9] = 0.124870209389901;

                      points1[10] = -3.02482471299611E-02;

                      points1[11] = 0;

                      points1[12] = 0.222752177630675;

                      points1[13] = -3.56861342544485E-02;

                      points1[14] = 0;

                      points1[15] = 0.21935349817787;

                      points1[16] = 0.111816553997272;

                      points1[17] = 0;

                      points1[18] = 0.201000629132725;

                      points1[19] = 0.230770334845434;

                      points1[20] = 0;

                      points1[21] = 4.55695676061768E-03;

                      points1[22] = 0.241646109094409;

                      points1[23] = 0;

                      points1[24] = -0.214318; ;

                      points1[25] = 0.244365052656653;

                      points1[26] = 0;

                      points1[27] = -0.214318;

                      points1[28] = 0.11045708221615;

                      points1[29] = 0;

                      points1[30] = -0.214318;

                      points1[31] = -4.52024367223014E-02;

                      points1[32] = 0;

                      pointArray = points1;

                      skSegment = Part.SketchManager.CreateSpline((pointArray));

                      myView = swDrawing.ActiveDrawingView;

                      longstatus = myView.Crop();

                      boolstatus = Part.Extension.SelectByID2("Spline1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                      longstatus = myView.Crop();

       

                      boolstatus = swDrawing.ActivateView("Чертежный вид1");

                      points2 = new double[27];

                      points2[0] = -4.24360470097527E-02;

                      points2[1] = -4.24834931600578E-02;

                      points2[2] = 0;

                      points2[3] = -2.92421794975378E-04;

                      points2[4] = -0.031607718911083;

                      points2[5] = 0;

                      points2[6] = 3.84525239669974E-02;

                      points2[7] = -3.77253419261313E-02;

                      points2[8] = 0;

                      points2[9] = 4.86485623254112E-02;

                      points2[10] = 5.74376827523982E-02;

                      points2[11] = 0;

                      points2[12] = 4.93282982159721E-02;

                      points2[13] = 0.210378258128606;

                      points2[14] = 0;

                      points2[15] = 1.06704998614657E-03;

                      points2[16] = 0.219894560596459;

                      points2[17] = 0;

                      points2[18] = -4.24360470097527E-02;

                      points2[19] = 0.22057429648702;

                      points2[20] = 0;

                      points2[21] = -4.24360470097527E-02;

                      points2[22] = 7.57905517975432E-02;

                      points2[23] = 0;

                      points2[24] = -4.24360470097527E-02;

                      points2[25] = -4.24834931600578E-02;

                      points2[26] = 0;

                      pointArray = points2;

                      skSegment = Part.SketchManager.CreateSpline((pointArray));

                      myView = swDrawing.ActiveDrawingView;

                      longstatus = myView.Crop();

                      boolstatus = Part.Extension.SelectByID2("Spline1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                      longstatus = myView.Crop();

       

                      boolstatus = swDrawing.ActivateView("Чертежный вид3");

                      points3 = new double[33];

                      points3[0] = -0.218673762287309;

                      points3[1] = -0.049280852065667;

                      points3[2] = 0;

                      points3[3] = -0.101079453220269;

                      points3[4] = -2.00522087715472E-02;

                      points3[5] = 0;

                      points3[6] = -4.7854141725201E-04;

                      points3[7] = -4.58821726128624E-02;

                      points3[8] = 0;

                      points3[9] = 0.104880521619691;

                      points3[10] = -2.27711523337909E-02;

                      points3[11] = 0;

                      points3[12] = 0.213638264109439;

                      points3[13] = -5.74376827523981E-02;

                      points3[14] = 0;

                      points3[15] = 0.214997735890561;

                      points3[16] = 8.73460619370789E-02;

                      points3[17] = 0;

                      points3[18] = 0.191206979720929;

                      points3[19] = 0.232129806626556;

                      points3[20] = 0;

                      points3[21] = 1.71945917373321E-02;

                      points3[22] = 0.236887957860482;

                      points3[23] = 0;

                      points3[24] = -0.218673762287309;

                      points3[25] = 0.236208221969921;

                      points3[26] = 0;

                      points3[27] = -0.213915611053382;

                      points3[28] = 8.19081748125915E-02;

                      points3[29] = 0;

                      points3[30] = -0.218673762287309;

                      points3[31] = -0.049280852065667;

                      points3[32] = 0;

                      pointArray = points3;

                      skSegment = Part.SketchManager.CreateSpline((pointArray));

                      myView = swDrawing.ActiveDrawingView;

                      longstatus = myView.Crop();

                      boolstatus = Part.Extension.SelectByID2("Spline1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                      longstatus = myView.Crop();

                      Part.ClearSelection2(true);

       

                  longstatus = Part.SaveAs3(@"C:\IM\IMWork\Направляющий аппарат с торовой поверхностью.DWG", 0, 2);

       

                      MessageBox.Show(@"Чертеж был успешно построен и сохранен в папку C:\IM\IMWORK\Направляющий аппарат с торовой поверхностью.DWG");

                  }

                  catch (Exception ex)

                  {

                      MessageBox.Show(ex.Message);

                  }

              }

          }

      }

       

       

      I will be very grateful for the help!