Close document without unloading it from memory?

Use this space to ask how to do whatever you're trying to use SolidWorks to do.
laukejas
Posts: 195
Joined: Sun Sep 05, 2021 8:27 am
Answers: 0
x 42
x 108

Close document without unloading it from memory?

Unread post by laukejas »

I have recently tried out chain Derived Part modelling technique (part A defines some features, part B imports part A and adds more features, part C imports part B and adds even more features, etc.), and it seems very powerful and reliable for very complex geometries that are simply too complex to keep in single part's history. However, for changes to propagate, all these documents (A, B, C) have to be loaded in SW memory, so I have System Options -> External References -> Load Referenced Documents set to All. This means I only have to open part C, and parts A and B are loaded automatically, ensuring that everything gets updated.

However, if I follow the following steps:
1) Open part C (A and B get loaded automatically);
2) Open part A and part B;
3) Close part B;
4) Modify part A;
5) Switch back to part C;

...Then part B shows ->? in the C's Feature Manager Tree, meaning it has been unloaded when I closed it, meaning changes from part A won't propagate to C.

Question - is it possible to close a document in SW without unloading it from RAM? Is there a setting I am unaware of?

Alternatively, if that is not possible, is there some API call to load all referenced documents of all open documents back into memory? I've searched for that, but couldn't find anything.
User avatar
JSculley
Posts: 638
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 868

Re: Close document without unloading it from memory?

Unread post by JSculley »

After modifying part A and returning to part C, File....Reload will update C with the changes in A. Before using reload though, make sure you save C so that any changes made in C aren't lost.
laukejas
Posts: 195
Joined: Sun Sep 05, 2021 8:27 am
Answers: 0
x 42
x 108

Re: Close document without unloading it from memory?

Unread post by laukejas »

JSculley wrote: Mon May 13, 2024 11:05 am After modifying part A and returning to part C, File....Reload will update C with the changes in A. Before using reload though, make sure you save C so that any changes made in C aren't lost.
That is what I do right now (or just close and reopen the document, which is the same), but for example if my active document is a large assembly containing part C, then it can take a very long time to reload everything, rather than just load back into memory these few documents that were closed. Basically I want to load all un-loaded referenced documents without having to reload everything. I should have mentioned this in my first post, sorry about that :D
User avatar
JSculley
Posts: 638
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 868

Re: Close document without unloading it from memory?

Unread post by JSculley »

laukejas wrote: Mon May 13, 2024 2:11 pm That is what I do right now (or just close and reopen the document, which is the same), but for example if my active document is a large assembly containing part C, then it can take a very long time to reload everything, rather than just load back into memory these few documents that were closed. Basically I want to load all un-loaded referenced documents without having to reload everything. I should have mentioned this in my first post, sorry about that :D
You can do this with a macro. Walk the feature tree calling getTypeName2 for each feature. Any feature whose type name is "Stock" is a derived part. Call getDefinition() for those features to get the DerivedPartFeatureData. You can call GetModelDoc() on the DerivedPartFeatureData and if it isn't null, you have the document loaded. If it is null, you have to load the document using OpenDoc7 and then you can set its Visible property to false so that it doesn't have an open document window. You can create a recursive method that will drill down into each loaded document to get any further derived parts. Here's a simple C# version:

Code: Select all

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;

namespace LoadAllRefs.csproj
{
    public partial class SolidWorksMacro
    {

        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;

        public void Main()
        {
            ModelDoc2 mDoc = swApp.ActiveDoc as ModelDoc2;
            traverseFeatures(mDoc);
        }

        

        private void traverseFeatures(ModelDoc2 mDoc)
        {
            FeatureManager fMgr = mDoc.FeatureManager;
            object[] featObjArray = fMgr.GetFeatures(false) as object[];
            foreach (object o in featObjArray)
            {
                Feature nextFeature = o as Feature;
                if (nextFeature.GetTypeName2().Equals("Stock"))
                {
                    DerivedPartFeatureData dpfd = nextFeature.GetDefinition() as DerivedPartFeatureData;
                    ModelDoc2 stockModelDoc = dpfd.GetModelDoc() as ModelDoc2;
                    if (stockModelDoc == null)
                    {
                        DocumentSpecification spec = swApp.GetOpenDocSpec(dpfd.PathName) as DocumentSpecification;
                        spec.Silent = true;
                        spec.DocumentType = (int)swDocumentTypes_e.swDocPART;
                        stockModelDoc = swApp.OpenDoc7(spec);
                        stockModelDoc.Visible = false;
                    }
                    traverseFeatures(stockModelDoc);
                }
            }
        }
    }
}

laukejas
Posts: 195
Joined: Sun Sep 05, 2021 8:27 am
Answers: 0
x 42
x 108

Re: Close document without unloading it from memory?

Unread post by laukejas »

JSculley wrote: Mon May 13, 2024 3:19 pm You can do this with a macro. Walk the feature tree calling getTypeName2 for each feature. Any feature whose type name is "Stock" is a derived part. Call getDefinition() for those features to get the DerivedPartFeatureData. You can call GetModelDoc() on the DerivedPartFeatureData and if it isn't null, you have the document loaded. If it is null, you have to load the document using OpenDoc7 and then you can set its Visible property to false so that it doesn't have an open document window. You can create a recursive method that will drill down into each loaded document to get any further derived parts. Here's a simple C# version:

Code: Select all

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;

namespace LoadAllRefs.csproj
{
    public partial class SolidWorksMacro
    {

        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;

        public void Main()
        {
            ModelDoc2 mDoc = swApp.ActiveDoc as ModelDoc2;
            traverseFeatures(mDoc);
        }

        

        private void traverseFeatures(ModelDoc2 mDoc)
        {
            FeatureManager fMgr = mDoc.FeatureManager;
            object[] featObjArray = fMgr.GetFeatures(false) as object[];
            foreach (object o in featObjArray)
            {
                Feature nextFeature = o as Feature;
                if (nextFeature.GetTypeName2().Equals("Stock"))
                {
                    DerivedPartFeatureData dpfd = nextFeature.GetDefinition() as DerivedPartFeatureData;
                    ModelDoc2 stockModelDoc = dpfd.GetModelDoc() as ModelDoc2;
                    if (stockModelDoc == null)
                    {
                        DocumentSpecification spec = swApp.GetOpenDocSpec(dpfd.PathName) as DocumentSpecification;
                        spec.Silent = true;
                        spec.DocumentType = (int)swDocumentTypes_e.swDocPART;
                        stockModelDoc = swApp.OpenDoc7(spec);
                        stockModelDoc.Visible = false;
                    }
                    traverseFeatures(stockModelDoc);
                }
            }
        }
    }
}

I was hoping there was a way to do this without a macro, but damn, you were quick. Thank you very, very much! I will test this and adapt it to work with assemblies as well.
Post Reply