7 Replies Latest reply on Nov 2, 2017 3:00 PM by Alex Burnett

    ImportDxfDwgData locking up SolidWorks

    Todd Carpenter

      Not sure what's going on, but I have a macro I've been using, and now when I use it, it locks up the drawing I'm inserting a DWG into.  I can move around in the drawing, but can't really do anything - including a rebuild.  I don't get any errors from SolidWorks or the code, but I have to kill SolidWorks using task manager to get it to come back to life.  It's always been a bit "iffy" in how it ran, but now I can't really even use it - running in debug hasn't shown me anything wrong either.  I added a GC.Collect statement in there in hopes it would maybe clean things up - but still no luck.  Can anybody tell me what I'm doing wrong here?  My process is to start with an empty drawing, and then run the macro, I then manually create a block out of the imported geometry.  Here's my code:

       

      using SolidWorks.Interop.sldworks;
      using SolidWorks.Interop.swconst;
      using System.Runtime.InteropServices;
      using System;
      using System.Windows.Forms;
      
      
      namespace InsertDWG.csproj
      {
          public partial class SolidWorksMacro
          {
              public void Main()
              {
                  //Get the file to import.
                  OpenFileDialog fdlg = new OpenFileDialog();
                  fdlg.Title = "Import DWG File Dialog";
                  fdlg.InitialDirectory = @"E:\TDCI3D\OHD\ComSect\_Docs\SectionB-B\";
                  fdlg.Filter = "DWG files (*.dwg)|*.dwg|All files (*.*)|*.*";
                  fdlg.FilterIndex = 1;
                  fdlg.RestoreDirectory = true;
      
      
                  if (fdlg.ShowDialog() == DialogResult.OK)
                  {
                      try
                      {
                          string sDwgFileName = fdlg.FileName;
      
      
                          ModelDoc2 swModel = default(ModelDoc2);
                          //ModelView swModelView = default(ModelView);
                          DrawingDoc swDraw = default(DrawingDoc);
                          FeatureManager swFeatMgr = default(FeatureManager);
                          Feature swFeat = default(Feature);
      
      
                          //SolidWorks.Interop.sldworks.View swView = default(SolidWorks.Interop.sldworks.View);
        
                          bool bRet = false;
                          ImportDxfDwgData importData = default(ImportDxfDwgData);
      
      
                          swModel = (ModelDoc2)swApp.ActiveDoc;
                          //swModelView = (ModelView)swModel.ActiveView;
      
      
                          bRet = swModel.Extension.SelectByID2("Sheet1", "SHEET", 0.0, 0.0, 0.0, false, 0, null, 0);
      
      
                          swDraw = (DrawingDoc)swModel;
                          swFeatMgr = swModel.FeatureManager;
                          importData = (ImportDxfDwgData)swApp.GetImportFileData(sDwgFileName);
      
      
                          // Unit
                          importData.set_LengthUnit("", (int)swLengthUnit_e.swINCHES);
      
      
                          // Position
                          bRet = importData.SetPosition("", (int)swDwgImportEntitiesPositioning_e.swDwgEntitiesCentered, 0.0, 0.0);
      
      
                          // Sheet scale
                          bRet = importData.SetSheetScale("", 1.0, 1.0);
      
      
                          // Paper size
                          bRet = importData.SetPaperSize("", (int)swDwgPaperSizes_e.swDwgPaperBsize, 0.0, 0.0);
      
      
                          //Import method
                          importData.set_ImportMethod("", (int)swImportDxfDwg_ImportMethod_e.swImportDxfDwg_ImportToExistingDrawing);
      
      
                          // Import file with importData
                          swFeat = swFeatMgr.InsertDwgOrDxfFile2(sDwgFileName, importData);
      
      
                          GC.Collect();
                          
                          return;
      
      
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show("Error: " + ex.Message);
                      }
                  }// end try
              }// end if
      
      
              /// <summary>
              ///  The SldWorks swApp variable is pre-assigned for you.
              /// </summary>
              public SldWorks swApp;
          }
      }
      
      
      
      

       

      Here's the message SolidWorks gives me when I try to do anything in the drawing:

       

      Thanks

        • Re: ImportDxfDwgData locking up SolidWorks
          Alex Burnett

          Todd Carpenter wrote:

           

          Not sure what's going on, but I have a macro I've been using, and now when I use it, it locks up the drawing I'm inserting a DWG into. I can move around in the drawing, but can't really do anything - including a rebuild. I don't get any errors from SolidWorks or the code, but I have to kill SolidWorks using task manager to get it to come back to life. It's always been a bit "iffy" in how it ran, but now I can't really even use it - running in debug hasn't shown me anything wrong either. I added a GC.Collect statement in there in hopes it would maybe clean things up - but still no luck. Can anybody tell me what I'm doing wrong here? My process is to start with an empty drawing, and then run the macro, I then manually create a block out of the imported geometry.

          Have you stepped through the lines of code with break points to see which line it hangs on?

          The first thing that come to mind istrying to use variables that haven't been set. The API isn't very good at letting you know when this happens. Typically when this happens to me, solidworks just crashes altogether unexpectedly though.

           

          For the sake of checking, instead of setting each SW object to default(objectName), could you set them to null? This will help track down if a command further in the code fails to actually set it to something because it will still be null.

           

          Then, before you use a variable to access a function under it, perform a null check:

          if(variable != null)

               Keep going;

          else

               Console.WriteLine("Variable was null");