AnsweredAssumed Answered

ImportDxfDwgData locking up SolidWorks

Question asked by Todd Carpenter on Nov 2, 2017
Latest reply on Nov 2, 2017 by Alex Burnett

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)
                    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);


                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: