4 Replies Latest reply: Sep 11, 2014 11:47 AM by Poorvesh Mistry

    Changing material properties of Components at the Assembly level

    John Girgenti

      Hello,

       

      I have bee trying change the Material Properties of the componets within an assembly but at the assembly level (part-instance@assembly) and have so far failed. I have been able to successfully change the properties at the part level through the assembly by selecting the parts and then using the assembly ModelDoc2.MaterialPropertyValues. If I select the parts by name, using the partname-instance "@" assemlby name convention nothing happens.  I can change the top level color for the assembly using the ModelDoc Extension.SetMaterialPropertyValues for the assemly.

       

      Code for changing assemlby top level Material Properties

      IModelDoc2 modDoc = default(IModelDoc2);


      // Get the current document

      modDoc = (IModelDoc2)iSwApp.ActiveDoc;   

       

      // Create a null object for SetMaterialPropertyValues

      object oNull = new object();

       

      // Set properties array [ R, G, B, Amb, Dif, Spec, Shine, Trans, Emis ]

      Double[] properties = { 1, .5, .25, 0.5, 0.5, 0, 0, 0, 0 };

       

      // Set the properties

      modDoc.Extension.SetMaterialPropertyValues(properties, (int)swInConfigurationOpts_e.swThisConfiguration, oNull);

      ...end code

       

      Code for changing part level Material Properties within an assembly

      IModelDoc2 modDoc = default(IModelDoc2);               

      IComponent2 swComp = default(IComponent2);

      IModelDoc2 compDoc = default(IModelDoc2);


      // Get the current document

      modDoc = (IModelDoc2)iSwApp.ActiveDoc;                                           

       

      // I'm just testing with one preselected part here

      // Note: preselection gets the part level, not assembly component level

      int count = modDoc.SelectionManager.GetSelectedObjectCount2(-1);

      if (count == 1)

      {

           // Get the component of the selection

           swComp = modDoc.SelectionManager.GetSelectedObjectsComponent3(1, -1);     

           // Get the modeldoc of the component

           compDoc = swComp.GetModelDoc2();                                          

           // Set properties array [ R, G, B, Amb, Dif, Spec, Shine, Trans, Emis ]

           Double[] properties = { 1, .5, .25, 0.5, 0.5, 0, 0, 0, 0 };


           compDoc.MaterialPropertyValues = properties;

      }

      ...end code

       

      I think I may have been looking at this too long. Does anyone know how to accomplish this?

      Thanks in advance!

       


        • Re: Changing material properties of Components at the Assembly level
          Keith Rice

          John,

           

          You might try using IComponent2::SetMaterialPropertyValues2 or IComponent2::MaterialPropertyValues rather than IModelDocExtension::MaterialPropertyValues.

           

          Keith

          Video Tutorials for the SolidWorks API

            • Re: Changing material properties of Components at the Assembly level
              John Girgenti

              HI Keith,

               

              Both of those methods worked, thank you!  Here is my code.

               

              Begin code...

              IModelDoc2 swModel = default(IModelDoc2);

              IModelDoc2 swCompModel = default(IModelDoc2);

              ISelectionMgr SelectionMgr = default(ISelectionMgr);         

              IComponent2 swComp = default(IComponent2);

               

              swModel = (IModelDoc2)iSwApp.ActiveDoc;

              SelectionMgr = (ISelectionMgr)swModel.SelectionManager;

              int count = SelectionMgr.GetSelectedObjectCount2(-1);

              if (count > 0)

              {

                   for (int i = 1; i <= count; i++)

                   {

                        // Get the component of the selection so we can get the name

                        swComp = SelectionMgr.GetSelectedObjectsComponent3(i, -1);

                        // Get the name of the component

                        string CompName = swComp.Name2;

                        // Create the assembly level component name, note fileName

                        // is derived elsewere, but it's the name of the assembly.

                        string modelLevelComponnet = CompName + "@" + fileName;

                        // Select the assebmly level component

                        swModel.SelectByName(0, modelLevelComponnet);

               

                        // Get the component of the selection at assembly level

                        swComp = SelectionMgr.GetSelectedObjectsComponent3(i, -1);

                        swCompModel = swComp.GetModelDoc2();

                        Double[] properties = { 1, .5, .25, 0.5, 0.5, 0, 0, 0, 0};                       

                        object oNull = new object();

               

                                // Set the properties

                        swComp.SetMaterialPropertyValues2(properties, _                (int)swInConfigurationOpts_e.swThisConfiguration, oNull);

                        //swComp.MaterialPropertyValues = properties;     // This works too

                     }

              }

              swModel.ClearSelection2(true);

              swModel.WindowRedraw();

              ...end code

               

              I do think I need to make seperate selection and modifiying loops yet, but the principal part is working.

              John

              • Re: Changing material properties of Components at the Assembly level
                Poorvesh Mistry

                Hi Keith,

                 

                I thought that this would change the material as in the sense Steel, Aluminum, Plastic, Rubber, but it appears that this is just for appearance (color)

                Could you provide me a code / macro to apply SolidWorks Library material like Steel, Aluminum, Plastic, Rubber, to parts in assembly, which would be reflected in BOM, but this should remain at assembly level.

                To explain more in detail, we have a huge list of standard components that we use, like pipes, elbows, flanges. These components are provided by SolidWorks in its library but does not have material defined in it. We use about four materials, say Stainless steel 304, 304L, 316 & 316L. Now we do not wish to create duplicate parts or extra configurations with different materials & then experiment on the routing database, so we thought if there is an easy way to apply custom property to parts at assembly level.

                 

                Just to explain more in detail

                1) We have "Part-1", which has no material applied on it

                2) There are many iterations / duplicates of "Part-1" in our assembly

                3) We need a macro that can do following

                    a) if parts in an assembly are selected in graphic window, start the macro, pops up a window, asks for selection of material from list of SolidWorks materials, hit Apply, applies selected material to selected components

                    b) if nothing or all selected, start the macro, pops up a window, asks for selection of material from list of SolidWorks materials, hit Apply, applies selected material to all components in the assembly

                4) This should not change the actual part, but still be a property for the part at assembly level, just like "Component Reference" value

                 

                Hope I was able to explain my intentions clearly & looking forward for a positive reply

              • Re: Changing material properties of Components at the Assembly level
                Oleg Bezyaev

                John

                1. foreach (double num in values) 
                2.             { 
                3.                 IModelDoc2 doc = SwApp.IActiveDoc2; 
                4.                 IPartDoc doc2 = (IPartDoc)doc; 
                5.                 double[] materialPropertyValues = (double[])doc.Extension.GetMaterialPropertyValues(2, null); 
                6.                 materialPropertyValues[0] = Red; 
                7.                 materialPropertyValues[1] = Green; 
                8.                 materialPropertyValues[2] = Blue; 
                9.                 doc.Extension.SetMaterialPropertyValues(materialPropertyValues, 2, null); 
                10.                 doc.WindowRedraw(); 
                11.             }