Macro to assign same configuration specific and material

Hello all,

I am new in API and only able to record some simple macros. I want to assign same material and configuration spec. for all parts in part properties using macro. Can anyone  help me to create a macro to assign configuration specific and material as shown below:

Is it possible  to make changes in complete folder with single run of macro?

Thanks in advance

Manjeet Ranjan

SolidworksApi macros
Answers 10
DG
DG 2010-12-14 Best Answer

Here is one for material Change_Part_Material which can be changed to set config properties also.

DG
DG 2010-12-14
Option Explicit
Dim swApp        As SldWorks.SldWorks
Dim swModel      As SldWorks.ModelDoc2
Dim sFileName As String
Dim silent As Boolean
Dim Path         As String
Dim nErrors      As Long
Dim nWarnings    As Long
Sub main()
    Set swApp = Application.SldWorks
     
    
    Path = "D:\SW\" ' Set you path here. If required browse function can be added
  
    sFileName = Dir(Path & "*.sldprt")
  
  
    Do Until sFileName = ""
    Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)
    Set swModel = swApp.ActiveDoc
  
'Change Default and Copper with your requirements
     swModel.SetMaterialPropertyName2 "Default", "SolidWorks Materials", "Copper"
     swModel.AddCustomInfo3 "Default", "Material", 30, """SW-Material"""
     swModel.AddCustomInfo3 "Default", "Weight", 30, """SW-Mass"""
  
        swModel.ForceRebuild3 False
  swModel.Save2 silent
     
   swApp.CloseDoc swModel.GetTitle
 
   Set swModel = Nothing
 
   sFileName = Dir
 
Loop
End Sub
SF
SF 2010-12-14

Hi Deepak,

Thank you for your effort.

I tried this MACRO but it did not work. Which path should be given here in programm

"Path = "D:\SW\" ' Set you path here. If required browse function can be added"

And also I want to assign the default material only so that I can get weight directly in File ->Properties ->Configuration specific using formula. All these properties should be assigned to the part only.

Procedure for what I want macro:

  Select a part (OR folder containing number of parts) then assign default material (Say plain carbon steel). Go to file -properties-summery information-configuration specific. Choose material in property name , text in type, Material in value from drop down menu. Do the same for Weight also. Please refer earlier attached images for more clarification.

<p>Deepak Gupta wrote:</p><p></p><div id="_mcePaste">Option Explicit</div><div id="_mcePaste"> </div><div id="_mcePaste">Dim swApp        As SldWorks.SldWorks</div><div id="_mcePaste">Dim swModel      As SldWorks.ModelDoc2</div><div>Dim sFileName As String</div><div id="_mcePaste">Dim silent As Boolean</div><div id="_mcePaste">Dim Path         As String</div><div id="_mcePaste">Dim nErrors      As Long</div><div id="_mcePaste">Dim nWarnings    As Long</div><div id="_mcePaste"> </div><div id="_mcePaste">Sub main()</div><div id="_mcePaste"> </div><div id="_mcePaste">    Set swApp = Application.SldWorks</div><div id="_mcePaste">      </div><div id="_mcePaste">     </div><div id="_mcePaste">    Path = "D:\SW\" ' Set you path here. If required browse function can be added</div><div id="_mcePaste">   </div><div id="_mcePaste">    sFileName = Dir(Path & "*.sldprt")</div><div id="_mcePaste">   </div><div id="_mcePaste">   </div><div id="_mcePaste">    Do Until sFileName = ""</div><div id="_mcePaste"> </div><div id="_mcePaste">    Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)</div><div id="_mcePaste"> </div><div id="_mcePaste">    Set swModel = swApp.ActiveDoc</div><div id="_mcePaste">   </div><div>'Change Default and Copper with your requirements</div><div> </div><div> </div><div id="_mcePaste"> </div><div id="_mcePaste"> </div><div id="_mcePaste">     swModel.SetMaterialPropertyName2 "Default", "SolidWorks Materials", "Copper"</div><div id="_mcePaste"> </div><div id="_mcePaste">     swModel.AddCustomInfo3 "Default", "Material", 30, """SW-Material"""</div><div id="_mcePaste">     swModel.AddCustomInfo3 "Default", "Weight", 30, """SW-Mass"""</div><div id="_mcePaste">   </div><div id="_mcePaste"> </div><div id="_mcePaste">        swModel.ForceRebuild3 False</div><div id="_mcePaste"> </div><div id="_mcePaste">  swModel.Save2 silent</div><div id="_mcePaste">      </div><div id="_mcePaste">   swApp.CloseDoc swModel.GetTitle</div><div id="_mcePaste">  </div><div id="_mcePaste">   Set swModel = Nothing</div><div id="_mcePaste">  </div><div id="_mcePaste">   sFileName = Dir</div><div id="_mcePaste">  </div><div id="_mcePaste">Loop</div><div> </div><div>End Sub</div><div> </div>

-Thanx.

DG
DG 2010-12-14
<p>Manjeet Ranjan wrote:</p><p></p><p>Hi Deepak,</p><p></p><p>Thank you for your effort.</p><p>I tried this MACRO but it did not work. Which path should be given here in programm</p><p><span><strong>"Path = "D:\SW\" ' Set you path here. If required browse function can be added</strong></span>"</p><p></p><p></p>

Replace "D:\SW\" with the folder location where parts are placed. It has to changed every time you need to work on parts on different folder location. I'll add up browse location function. Make sure your path in within quotes and \ at the end of folder name (similar to the path I have in the macro)

Currently the macro will add copper as material, then add two config specific properties viz. Material and Weight but only to configuration named "Default".

If you need the macro can be edited to add config properties to active configuration.

Test the macro once again and after that I can incorporate the changes.

SF
SF 2010-12-14

Hello Deepak,

Its working fine now. I am Waiting for add config. macro. I more help what is the simple way to do the programming for macros.

Thanx again.

<p>Deepak Gupta wrote:</p><p></p><blockquote class="jive-quote"><p>Manjeet Ranjan wrote:</p><p></p><p>Hi Deepak,</p><p></p><p>Thank you for your effort.</p><p>I tried this MACRO but it did not work. Which path should be given here in programm</p><p><span><strong>"Path = "D:\SW\" ' Set you path here. If required browse function can be added</strong></span>"</p><p></p><p></p></blockquote><p>Replace <strong>"D:\SW\" </strong>with the folder location where parts are placed. It has to changed every time you need to work on parts on different folder location. I'll add up browse location function. Make sure your path in within quotes and \ at the end of folder name (similar to the path I have in the macro)</p><p></p><p>Currently the macro will add copper as material, then add two config specific properties viz. Material and Weight but only to configuration named "Default".</p><p></p><p>If you need the macro can be edited to add config properties to active configuration. </p><p></p><p>Test the macro once again and after that I can incorporate the changes.</p>
DG
DG 2010-12-14

The macro I stated above will add configuration specific properties provided there is Default (configuration name) configuration.

If you need more than this let me know like, browse function to select the folder/file location rather than editing the macro. Then this macro works for Default (configuration name) configuration at the moment. It can be edited to active configuration as required.

To learn API, first you need to learn VBA, .NET or C language. Then there are some good books available to master further.

DG
DG 2010-12-15

Try the attached one

SF
SF 2010-12-15

Its Awsome !

SF
SF 2010-12-16

Hello Deepak,

I have no. of parts in one assembly folder which is having different materials. I have made an excel sheet consisting of file name and their respective materials. Is it possible to connect the excel sheet to this MACRO for selecting materials? 

In macro you have made two programms one for browsing and another for config. and I am not able to edit browser programm.

DG
DG 2010-12-16

A list can be inbuilt in macro to select material from or can be linked to an external file say text. And don't edit the codes for browse function as then it'll not work.