12 Replies Latest reply on Aug 28, 2017 10:57 AM by Nadav Gover

    Open a part invisibly while drawing is already open

    Nadav Gover

      Hi,

       

      Does anyone have any idea of how to open a part in the background while a drawing is already opened?

      I'm trying this: 2017 SOLIDWORKS API Help - Keep SOLIDWORKS Invisible While Activating Documents Example VB

      but it seems to work only if SolidWorks is closed (and a drawing is not open).

      Any ideas?

       

      Thanks

        • Re: Open a part invisibly while drawing is already open
          Alex Burnett

          This thread may have the information you're looking for.

           

          https://forum.solidworks.com/thread/81450

            • Re: Open a part invisibly while drawing is already open
              Nadav Gover

              I saw this and thought it would be the answer, but with no luck

                • Re: Open a part invisibly while drawing is already open
                  Alex Burnett

                  If you're just looking for a way to get the referenced model that the drawing uses then the following code can be translated from c# to VBA and work. The model.AsDrawing() line is part of a plugin that I'm using but if you get the drawing object as a DrawingDoc object then this does the same thing. The rest should translate fairly well.

                   

                  once you have the refModelDoc2 object, then you can sort through the contents of the file without actually activating the document. I used it here to obtain the mass properties of the referenced file to determine if the custom property for the weight in the same file is accurate and in the correct units.

                   

                   

                  var swDraw = model.AsDrawing();
                  var swSheet = (Sheet)swDraw.GetCurrentSheet();
                  var sheetView = (SolidWorks.Interop.sldworks.View)swDraw.GetFirstView(); // Gets current drawing sheet as "view"
                  var modelView = (SolidWorks.Interop.sldworks.View)sheetView?.GetNextView(); // Gets first view on sheet
                  if (swSheet.CustomPropertyView != "Default") // Checks if the referenced view that the drawing uses to get custom properties is default
                  {
                       while (modelView.Name != swSheet.CustomPropertyView)
                       {
                            modelView = (View)modelView.GetNextView();  // sets modelView to the view that is referenced for custom properties
                       }
                  }
                  var refModelDoc2 = modelView?.ReferencedDocument; // Gets referenced ModelDoc2 item from the view on the sheet
                  
                    • Re: Open a part invisibly while drawing is already open
                      Nadav Gover

                      Thanks Alex it is also what i tried (I'm also using c#)

                      but the problem is that I can't get the feature manager and extension of the part to work.

                      the feature manager is the drawing's manager.

                      Do you have any idea?

                      Thanks

                        • Re: Open a part invisibly while drawing is already open
                          Alex Burnett

                          I tested out using the refModelDoc2 object from my code to iterate through the feature tree and it worked with this code snippet I found online. I'm not sure why yours isn't working. I'm not using the FeatureManager like you are here but I made sure that I could set it to something and it was not null. It contained all of the variables I would expect it to have.

                           

                          Code:

                          for(int x=0; x< refModelDoc2.GetFeatureCount(); x++)
                          {
                               Feature swFeat = (Feature)refModelDoc2.FeatureByPositionReverse(x);
                               if(swFeat != null)
                               {
                                    Trace.WriteLine("Feature: " + swFeat.Name + " [" + swFeat.GetTypeName() + "]");
                               }
                          }
                          

                           

                          Output:

                          Feature: M1.2x0.25 Tapped Hole1 [HoleWzd]
                          Feature: Point1 [RefPoint]
                          Feature: Coordinate System1 [CoordSys]
                          Feature: Axis1 [RefAxis]
                          Feature: Cut-Extrude1 [Cut]
                          Feature: Sketch2 [ProfileFeature]
                          Feature: Boss-Extrude1 [Extrusion]
                          Feature: Sketch1 [ProfileFeature]
                          Feature: Tables [ConfigTableFolder]
                          Feature: MateReferences___EndTag___ [FtrFolder]
                          Feature: Default-<1> [PosGroupFolder]
                          Feature: MateReferences [MateReferenceGroupFolder]
                          Feature: Origin [OriginProfileFeature]
                          Feature: Right Plane [RefPlane]
                          Feature: Top Plane [RefPlane]
                          Feature: Front Plane [RefPlane]
                          Feature: 1060-O   [MaterialFolder]
                          Feature: Equations [EqnFolder]
                          Feature: Lights, Cameras and Scene [EnvFolder]
                          Feature: Solid Bodies [SolidBodyFolder]
                          Feature: Surface Bodies [SurfaceBodyFolder]
                          Feature: Annotations [DetailCabinet]
                          Feature: Design Binder [DocsFolder]
                          Feature: Sensors [SensorFolder]
                          Feature: Selection Sets [SelectionSetFolder]
                          Feature: History [HistoryFolder]
                          Feature: Favorites [FavoriteFolder]
                          Feature: Comments [CommentsFolder]
                          
                          • Re: Open a part invisibly while drawing is already open
                            Josh Brady

                            Nadav,

                            Every document always has its own feature manager and its own extension.  You have to get the feature manager or extension from the ModelDoc2 you're interested in.  You have to do something like:

                             

                            dim myDwg as sldworks.drawingdoc

                            dim myDoc as sldworks.modeldoc2

                            dim myView as sldworks.View

                            dim myMgr as sldworks.featuremanager

                            dim myExt as sldworks.modeldocextension

                            ....

                            set myDwg = swapp.activedoc

                            set myView = myDwg.activeview

                            set myDoc = myView.ReferencedDocument

                            set myMgr = myDoc.FeatureManager

                            set myExt = myDoc.Extension

                    • Re: Open a part invisibly while drawing is already open
                      Deepak Gupta

                      The model always open invisibly when you open a drawing. So what exactly you looking to do with the model?

                      • Re: Open a part invisibly while drawing is already open
                        John Alexander

                        When a drawing is open that contains a view of a part/assembly, I believe the assembly/part documents are already loaded in memory. You can access them through the drawing's view object in which they are depicted.

                         

                        2012 SOLIDWORKS API Help - ReferencedDocument Property (IView)

                         

                        There might be a better approach that doesn't require views on the drawing but this has been my method for most drawing macros that I've worked on.

                        • Re: Open a part invisibly while drawing is already open
                          Nadav Gover

                          Thanks guys it's solved thanks to you.

                          The answer was a combination of Alex's and Josh's answers.

                          I was able to get rid of "ActivateDoc2" which opened the part visually.

                          I now use the FaetureManager from the ReferencedDocument.

                          Thank you yessss