Retrieving Custom Properties for Part when Drawing is Active

Programming and macros
User avatar
Sean Bennington
Posts: 5
Joined: Mon Jul 12, 2021 2:26 pm
Answers: 0
x 1
x 2

Retrieving Custom Properties for Part when Drawing is Active

Unread post by Sean Bennington »

I am developing an add-in to help our engineering team automate a portion of their workflow. I am not a Solidworks user, so I am not very familiar with this tool, but I do have extensive experience developing application code against an SDK. If I am not using the appropriate terminology I apologize :-)

I have been able to retrieve the custom properties from the Summary Information dialog for the current drawing (See code snippet below).
I am having trouble accessing the same properties for a part defined in a named view existing in a sheet of the drawing. What I have noticed is that the Summary Information dialog appears to be context-sensitive.

If the drawing is active, the drawing custom property values are available from the File->Properties menu. If one of the parts is active then the part's custom properties are available via the File->Properties menu. The automation tool I am building needs to be able to fetch the part custom properties from the context of an active drawing.

How do I get the custom properties for a part when the drawing is the active document?

Code: Select all

            Dictionary<string, string> customProperties = new Dictionary<string, string>
            {
                { "Description", "" },
                { "Description2", "" },
                { "Drawn By", "" },
                { "Date", "" }
            };

            ModelDoc2 swModel = default(ModelDoc2);
            IModelDocExtension swModelDocExt = default(IModelDocExtension);
            CustomPropertyManager swCustProp = default(CustomPropertyManager);

            string val = "";
            string valout = "";

            swModel = (ModelDoc2)App.ActiveDoc;
            swModelDocExt = swModel.Extension;

            swCustProp = swModelDocExt.get_CustomPropertyManager("");

            foreach (KeyValuePair<string, string> entry in customProperties.ToList())
            {
                if (swCustProp.Get4(entry.Key, false, out val, out valout))
                {
                    customProperties[entry.Key] = val;
                }
            }

            return customProperties;
by Eddy Alleman » Tue Jul 13, 2021 5:05 am
Hi Sean,

You first have to retrieve the Modeldoc2 of the 3D model the drawing is referring to:

1) Get the "Sheet"
Sheet swSheet = (Sheet)swDrawing?.GetCurrentSheet();
and use "CustomPropertyView" to get the name of the view the custom properties are used for.
Iterate over all views and get the corresponding "View" object (swView)

2) Then use "ModelDoc2 swModelInView = swView.ReferencedDocument;" to get the modeldoc of the 3D model (model3D).

3)Once you have that you can use the same function, as you used before:
model3D.Extension.get_CustomPropertyManager(configname);
if configname is "" you get the file custom properties of the 3D model.
if you want to get the configuration custom properties, then use the configuration name there.

I hope this gets you started.

Happy coding ;-)

Eddy
Go to full post
User avatar
Eddy Alleman
Posts: 50
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 80
x 89
Contact:

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by Eddy Alleman »

Hi Sean,

You first have to retrieve the Modeldoc2 of the 3D model the drawing is referring to:

1) Get the "Sheet"
Sheet swSheet = (Sheet)swDrawing?.GetCurrentSheet();
and use "CustomPropertyView" to get the name of the view the custom properties are used for.
Iterate over all views and get the corresponding "View" object (swView)

2) Then use "ModelDoc2 swModelInView = swView.ReferencedDocument;" to get the modeldoc of the 3D model (model3D).

3)Once you have that you can use the same function, as you used before:
model3D.Extension.get_CustomPropertyManager(configname);
if configname is "" you get the file custom properties of the 3D model.
if you want to get the configuration custom properties, then use the configuration name there.

I hope this gets you started.

Happy coding ;-)

Eddy
User avatar
Sean Bennington
Posts: 5
Joined: Mon Jul 12, 2021 2:26 pm
Answers: 0
x 1
x 2

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by Sean Bennington »

YES!!!

Thank you Eddy, this was exactly what I needed to get the custom properties for the part. I do have a follow-up question regarding the CustomPropertyView property of the ISheet interface.

When I collected the view name string currently assigned to the ISheet.CustomPropertyView property, the value returned for the view name was "default". When iterating over the collection of views returned by the ISheet.GetViews method, "default" was not in the list of views. Is there some administration process that needs to happen by the Engineers creating the drawing to make an assignment to one of the available views so that this property works correctly?

I kludged this process by selecting the first named-view from the collection of views. It has the custom properties I was looking for but this is a fragile implementation, and I would prefer to get it working the way you described it.


-Sean
User avatar
Eddy Alleman
Posts: 50
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 80
x 89
Contact:

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by Eddy Alleman »

Good to hear that!

Eddy

EDIT
I do have a follow-up question regarding the CustomPropertyView property of the ISheet interface.

When I collected the view name string currently assigned to the ISheet.CustomPropertyView property, the value returned for the view name was "default". When iterating over the collection of views returned by the ISheet.GetViews method, "default" was not in the list of views. Is there some administration process that needs to happen by the Engineers creating the drawing to make an assignment to one of the available views so that this property works correctly?

I kludged this process by selecting the first named-view from the collection of views. It has the custom properties I was looking for but this is a fragile implementation, and I would prefer to get it working the way you described it.


-Sean
Next time, Please add a new post or add EDIT before the additional questions. That will make it easier for others to follow.
Anyway Alex has a good solution for you below:

Eddy
User avatar
mattpeneguy
Posts: 1386
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2489
x 1899

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by mattpeneguy »

Sean Bennington wrote: Wed Jul 14, 2021 11:52 am YES!!!

Thank you Eddy, this was exactly what I needed to get the custom properties for the part. I do have a follow-up question regarding the CustomPropertyView property of the ISheet interface.

When I collected the view name string currently assigned to the ISheet.CustomPropertyView property, the value returned for the view name was "default". When iterating over the collection of views returned by the ISheet.GetViews method, "default" was not in the list of views. Is there some administration process that needs to happen by the Engineers creating the drawing to make an assignment to one of the available views so that this property works correctly?

I kludged this process by selecting the first named-view from the collection of views. It has the custom properties I was looking for but this is a fragile implementation, and I would prefer to get it working the way you described it.


-Sean
I'm not a programmer, but I have been known to kludge things together from time to time. Can you post a working example with the code in question for me to look through? I can run it on my machine and see if I can figure out what's going on...And, it'll give people like @Eddy Alleman, that know what they are doing, more information to give you more targeted advice.
The "default" you mention has me questioning whether you are actually accessing the "configuration name" of the part instead. Since you aren't familiar with SW, parts and assemblies can have multiple configurations where sizes, appearances, etc. can be different. Every part and asm starts with a "default" configuration named, "default"...it's just a hunch.
Also, I'll ping a few of the programmers that sometimes help out with things like this. Hey @gupta9665 and @josh, care to weigh in?
User avatar
AlexB
Posts: 501
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 269
x 445

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by AlexB »

Sean Bennington wrote: Wed Jul 14, 2021 11:52 am YES!!!

Thank you Eddy, this was exactly what I needed to get the custom properties for the part. I do have a follow-up question regarding the CustomPropertyView property of the ISheet interface.

When I collected the view name string currently assigned to the ISheet.CustomPropertyView property, the value returned for the view name was "default". When iterating over the collection of views returned by the ISheet.GetViews method, "default" was not in the list of views. Is there some administration process that needs to happen by the Engineers creating the drawing to make an assignment to one of the available views so that this property works correctly?

I kludged this process by selecting the first named-view from the collection of views. It has the custom properties I was looking for but this is a fragile implementation, and I would prefer to get it working the way you described it.


-Sean
Here's how I did it in one of my add-ins. The "Default" view is the first view that's dropped on the first sheet unless otherwise specified. If it is named anything else, this will loop until it finds the correctly named view

c# code snippet

Code: Select all

object[] allSheetViewArrays = (object[])swDraw.GetViews();
for (int i = 0; i < allSheetViewArrays.Count(); i++)
{
    object[] sheetViews = (object[])allSheetViewArrays[i];
    for (int j = 0; j < sheetViews.Count(); j++)
    {
        v = (View)sheetViews[j];
        if ((swSheet.CustomPropertyView == "Default") ||
            swSheet.CustomPropertyView == v.Name)
        {
            refModelDoc2 = v.ReferencedDocument;
            if (refModelDoc2 != null)
            {
                break;
            }
        }
    }
    if (refModelDoc2 != null)
    {
        break;
    }
}
User avatar
Eddy Alleman
Posts: 50
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 80
x 89
Contact:

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by Eddy Alleman »

mattpeneguy wrote: Wed Jul 14, 2021 1:56 pm The "default" you mention has me questioning whether you are actually accessing the "configuration name" of the part instead. Since you aren't familiar with SW, parts and assemblies can have multiple configurations where sizes, appearances, etc. can be different. Every part and asm starts with a "default" configuration named, "default"...it's just a hunch.
The default means you want the default behavior:

from the help files for the sheet:
image.png
Use custom property values from model shown in
If more than one model is shown on the sheet and the drawing contains notes that are linked to custom properties of a model, select the view that contains the model whose properties you want to use. If you do not specify otherwise, the properties of the model in the first view inserted into the sheet are used. Or, select Same as sheet specified in Document Properties if Use custom property values from this sheet on all sheets is selected on the Drawing Sheets document property.

document properties involved and described above:
image.png
Cheers

Eddy
User avatar
gupta9665
Posts: 410
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 433
x 449

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by gupta9665 »

Thank you @mattpeneguy for the shout out.

@Sean Bennington, like @Eddy Alleman stated that using CustomPropertyView method can get you the view name used for the sheet. And then you can select the view by it's name using SelectByID2 in case you do not want to loop thru all views on the sheet.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
Sean Bennington
Posts: 5
Joined: Mon Jul 12, 2021 2:26 pm
Answers: 0
x 1
x 2

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by Sean Bennington »

I apologize for not jumping in with the requested information right away, I was out sick yesterday. Thank you very much for all of your help! Once I have read through all of the replies, and understand all that was said, I will be sure to post my complete solution for the sake of others who may run into this and just want to see the complete code.
User avatar
Sean Bennington
Posts: 5
Joined: Mon Jul 12, 2021 2:26 pm
Answers: 0
x 1
x 2

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by Sean Bennington »

Here is the solution I came up with. Please provide any appropriate critiques :-)

Code: Select all

	//Sample Usage:
	    ModelDoc2 swModel = (ModelDoc2)App.ActiveDoc;
            Dictionary<string, string> customProperties = GetCustomProperties(GetModelFromView(swModel));

	// Function Definitions
	
        public ModelDoc2 GetModelFromView(ModelDoc2 swModel)
        {
            ModelDoc2 swModelInView = default(ModelDoc2);

            DrawingDoc drawingDoc = (DrawingDoc)swModel;
            Sheet sheet = (Sheet)drawingDoc.GetCurrentSheet();

            // Get model views
            object[] modelViews = (object[])sheet.GetViews();

            if (sheet.CustomPropertyView == "Default")
            {
                // If you do not specify otherwise (hence "default"), the properties of the model 
                // in the first view inserted into the sheet are used
                swModelInView = ((View)modelViews[0]).ReferencedDocument;
            }
            else
            {
                // The default view is not being used. We need to find the view that contains the 
                // model with the properties. 
                foreach (IView view in modelViews)
                {
                    if (sheet.CustomPropertyView == view.GetName2())
                    {
                        swModelInView = view.ReferencedDocument;
                        break;
                    }
                }
            }

            return swModelInView;
        }

        private Dictionary<string, string> GetCustomProperties(ModelDoc2 swModel)
        {
            IModelDocExtension swModelDocExt = default(IModelDocExtension);
            CustomPropertyManager swCustProp = default(CustomPropertyManager);

            string val = "";
            string valout = "";

            Dictionary<string, string> customProperties = new Dictionary<string, string>
            {
                { "Description", "" },
                { "Description 2", "" },
                { "Drawn By", "" },
                { "Date", "" }
            };

            // Get the IModelDocExtension interface
            swModelDocExt = swModel.Extension;

            // Get the CustomPropertyManager from IModelDocExtension
            swCustProp = swModelDocExt.get_CustomPropertyManager("");

            foreach (KeyValuePair<string, string> entry in customProperties.ToList())
            {
                if (swCustProp.Get4(entry.Key, false, out val, out valout))
                {
                    customProperties[entry.Key] = val;
                }
            }

            return customProperties;
        }

@gupta9665 I did not understand how to get rid of the IView iteration step by using the SelectByID2 interface since this requires using the IModelDocExtension interface, which needs the correct IModelDoc2 instance, which is what I was looking for in the first place. I suspect I am missing something obvious :-) Can you elaborate a bit on how to use the SelectByID2?
User avatar
gupta9665
Posts: 410
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 433
x 449

Re: Retrieving Custom Properties for Part when Drawing is Active

Unread post by gupta9665 »

Sean Bennington wrote: Fri Jul 16, 2021 9:17 am
@gupta9665 I did not understand how to get rid of the IView iteration step by using the SelectByID2 interface since this requires using the IModelDocExtension interface, which needs the correct IModelDoc2 instance, which is what I was looking for in the first place. I suspect I am missing something obvious :-) Can you elaborate a bit on how to use the SelectByID2?

@Sean Bennington, with that view you can get the model (ModelDoc2) referenced in the sheet/drawing and then can work further. My idea was only to reduce the time by avoiding iteration of all the views.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
Post Reply