• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

macro: help me create a simple material change and look at the parts?

  • Thread starter Thread starter reggio
  • Start date Start date

reggio

Guest
hi, I find myself with an iges set of a client from which I echo together and sw parts but aspects and materials have lost, then to manina, one by one stage assigning material and changing appearance (color) ...

I tried to record a macro, but when I relaunch it with the intention to apply the same steps to a new part, obviously the macro repeats me the operations on the original part... :(

Do you think it is easy enough to explain how to change this macro to make sure that at each launch, the macro asks to select the (or maybe even more than one?) part to which to apply the changes?
 
Why don't you create a model with already set material (with color I don't know if it works) and when it amounts the template use that template?
 
vake also for color. given the material to in the appearance tab and change. saving and good import
 
... too easy...
When I get the bodies and sub-axis of bodies and not everything must become yellow steel, on an example of 100 bodies/sub-axis we will have
65% steel - yellow
20% galvanized sheet - galvanized
5% polyzene - black
5% c40 turned - red
5% commercial & various

I wanted to create 2/3 macros to assign speeding...

You say it's not that easy to explain to me?
 
Try this, copy it and make it as many as you need it according to the materials you want to associate.
prerequisites:
- open file
- pre-select one or more components (just click on a component face, ctrl pressed for multi selection, as you usually do)
- launches the macro

change the macro to pleasure by changing the material to associate, if you like the color of the material of solidworks comments (in programming jargon means putting the apex) on the rows you do not care about)otherwise change the color to associate on the first 3 rows of the retvar(0) = 1 (would be the value of the divided rgb for 255)





Red and 1.5714 (16nicr4)
dim swapp as sldworks.sldworks
dim modeldoc2 as sldworks.modeldoc2
dim swcompmodel as sldworks.modeldoc2
dim swmodel as sldworks.modeldoc2
dim swselmgr as sldworks.selectionmgr
dim swcomp as sldworks.component2
dim vmatprop as variant
dim bret as boolean
dim longstatus as long, longwarnings as long
global const swdocpart = 1
global const swdocasm = 2
dim retvar as variant
sub main()
set swapp = createobject("sldworks.application")
set modeldoc2 = swapp.activedoc
if modeldoc2 is nothing then
msgbox "no document loaded." + chr$(10) + chr$(13) _
+ "Download a side file or together. "
exit sub
end if

filetyp = modeldoc2.gettype
if filetyp = swdocpart then
modeldoc2.materialpropertyvalues = retvar
modeldoc2.editrebuild3
end if

if filetyp = swdocasm then
set swmodel = swapp.activedoc

dim i as integer
dim selcount as integer

set swselmgr = swmodel.selectionmanager
selcount = swselmgr.getselectedobjectcount()
if selcount

for i = 1 to swselmgr.getselectedobjectcount()
set swselobj = swselmgr.getselectedobject5(s)
set swcomp = swselmgr.getselectedobjectscomponent2(i)
set swcompmodel = swcomp.getmodeldoc

dim partdoc as sldworks.partdoc
set partdoc = swcomp.getmodeldoc
'change material wording according to the need, if it also serves the path of the library
partdoc.setmaterialpropertyname2 "default", "c:/program files/solidworks corp/solidworks/lang/italian/sldmaterials/solidworks din materials.sldmat", "1.5714 (16nicr4)"

'in case you would not want to force the color of the material comment the code until "end comment"
'This part associates the red color to the part indifferently from the color of the material set in the material card si solidworks
vmatprop = swcomp.materialpropertyvalues
if isempty(vmatprop) then
if swcompmodel is nothing then
exit sub
end if
vmatprop = swcompmodel.materialpropertyvalues
end if
retvar = partdoc.materialpropertyvalues

retvar(0) = 1
retvar(1) = 0
retvar(2) = 0
retvar(3) = 1
retvar(4) = 1
retvar(5) = 1
retvar(6) = 0.31
retvar(7) = 0
retvar(8) = 0

partdoc.materialpropertyvalues = retvar
'"fine comment"
partdoc.save2 true
next
end if
swmodel.clearselection2 true
end if
end
 
ciao, ho trovato questa discussione a seguito di una ricerca.

non sono riuscito a farla funzionare, ho escluso (spero correttamente) il cambio di colore, ma il resto non mi funziona, allego il testo della macro da me modificata, se hai tempo jenuary, puoi darmi un occhio?

ti ringrazio!

'associa colore rosso e 1.5714 (16nicr4)
dim swapp as sldworks.sldworks
dim modeldoc2 as sldworks.modeldoc2
dim swcompmodel as sldworks.modeldoc2
dim swmodel as sldworks.modeldoc2
dim swselmgr as sldworks.selectionmgr
dim swcomp as sldworks.component2
dim vmatprop as variant
dim bret as boolean
dim longstatus as long, longwarnings as long
global const swdocpart = 1
global const swdocasm = 2
dim retvar as variant
sub main()
set swapp = createobject("sldworks.application")
set modeldoc2 = swapp.activedoc
if modeldoc2 is nothing then
msgbox "nessun documento caricato." + chr$(10) + chr$(13) _
+ "carica un file di parte o assieme."
exit sub
end if

filetyp = modeldoc2.gettype
if filetyp = swdocpart then
modeldoc2.materialpropertyvalues = retvar
modeldoc2.editrebuild3
end if

if filetyp = swdocasm then
set swmodel = swapp.activedoc

dim i as integer
dim selcount as integer

set swselmgr = swmodel.selectionmanager
selcount = swselmgr.getselectedobjectcount()
if selcount > 0 then

for i = 1 to swselmgr.getselectedobjectcount()
set swselobj = swselmgr.getselectedobject5(i)
set swcomp = swselmgr.getselectedobjectscomponent2(i)
set swcompmodel = swcomp.getmodeldoc

dim partdoc as sldworks.partdoc
set partdoc = swcomp.getmodeldoc
'cambiare dicitura materiale a seconda dell'esigenza, se serve anche il percorso della libreria
partdoc.setmaterialpropertyname2 "default", "c:\program files\solidworks corp\solidworks\lang\italian\sldmaterials/solidworks materials.sldmat", "aisi 316l"


"vmatprop = swcomp.materialpropertyvalues
if isempty(vmatprop) then
if swcompmodel is nothing then
exit sub
end if
vmatprop = swcompmodel.materialpropertyvalues
end if
retvar = partdoc.materialpropertyvalues

retvar(0) = 1
retvar(1) = 0
retvar(2) = 0
retvar(3) = 1
retvar(4) = 1
retvar(5) = 1
retvar(6) = 0.31
retvar(7) = 0
retvar(8) = 0

partdoc.materialpropertyvalues = retvar"
'"fine commento"
partdoc.save2 true
next
end if
swmodel.clearselection2 true
end if
end sub
 
cosi dovrebbe andare

'associa colore rosso e 1.5714 (16nicr4)
dim swapp as sldworks.sldworks
dim modeldoc2 as sldworks.modeldoc2
dim swcompmodel as sldworks.modeldoc2
dim swmodel as sldworks.modeldoc2
dim swselmgr as sldworks.selectionmgr
dim swcomp as sldworks.component2
dim vmatprop as variant
dim bret as boolean
dim longstatus as long, longwarnings as long
global const swdocpart = 1
global const swdocasm = 2
dim retvar as variant
sub main()
set swapp = createobject("sldworks.application")
set modeldoc2 = swapp.activedoc
if modeldoc2 is nothing then
msgbox "nessun documento caricato." + chr$(10) + chr$(13) _
+ "carica un file di parte o assieme."
exit sub
end if

filetyp = modeldoc2.gettype
if filetyp = swdocpart then
modeldoc2.materialpropertyvalues = retvar
modeldoc2.editrebuild3
end if

if filetyp = swdocasm then
set swmodel = swapp.activedoc

dim i as integer
dim selcount as integer

set swselmgr = swmodel.selectionmanager
selcount = swselmgr.getselectedobjectcount()
if selcount > 0 then

for i = 1 to swselmgr.getselectedobjectcount()
set swselobj = swselmgr.getselectedobject5(i)
set swcomp = swselmgr.getselectedobjectscomponent2(i)
set swcompmodel = swcomp.getmodeldoc

dim partdoc as sldworks.partdoc
set partdoc = swcomp.getmodeldoc
'cambiare dicitura materiale a seconda dell'esigenza, se serve anche il percorso della libreria
partdoc.setmaterialpropertyname2 "default", "c:\program files\solidworks corp\solidworks\lang\italian\sldmaterials/solidworks materials.sldmat", "aisi 316l"


'vmatprop = swcomp.materialpropertyvalues
'if isempty(vmatprop) then
'if swcompmodel is nothing then
'exit sub
'end if
'vmatprop = swcompmodel.materialpropertyvalues
'end if
'retvar = partdoc.materialpropertyvalues

'retvar(0) = 1
'retvar(1) = 0
'retvar(2) = 0
'retvar(3) = 1
'retvar(4) = 1
'retvar(5) = 1
'retvar(6) = 0.31
'retvar(7) = 0
'retvar(8) = 0

'partdoc.materialpropertyvalues = retvar"
'"fine commento"
partdoc.save2 true
next
end if
swmodel.clearselection2 true
end if
end sub
 
Yes, it works, thank you very much!

it would also be interesting that the color that was assigned to the part was maintained after the change.
 
that is part of the material color in the solidworks library.
otherwise it should be stored at the macro start, you change the material (then the color changes) and then overwrites the color with the one stored at the beginning.
 
that is part of the material color in the solidworks library.
otherwise it should be stored at the macro start, you change the material (then the color changes) and then overwrites the color with the one stored at the beginning.
and doing this is possible?
 
prova



'associa 1.5714 (16nicr4), il colore rimane lo stesso
dim swapp as sldworks.sldworks
dim modeldoc2 as sldworks.modeldoc2
dim swcompmodel as sldworks.modeldoc2
dim swmodel as sldworks.modeldoc2
dim swselmgr as sldworks.selectionmgr
dim swcomp as sldworks.component2
dim vmatprop as variant
dim bret as boolean
dim longstatus as long, longwarnings as long
global const swdocpart = 1
global const swdocasm = 2
dim retvar as variant
dim retvar_old as variant

sub main()
set swapp = createobject("sldworks.application")
set modeldoc2 = swapp.activedoc
if modeldoc2 is nothing then
msgbox "nessun documento caricato." + chr$(10) + chr$(13) _
+ "carica un file di parte o assieme."
exit sub
end if
filetyp = modeldoc2.gettype
if filetyp = swdocpart then
modeldoc2.materialpropertyvalues = retvar
modeldoc2.editrebuild3
end if
if filetyp = swdocasm then
set swmodel = swapp.activedoc
dim i as integer
dim selcount as integer
set swselmgr = swmodel.selectionmanager
selcount = swselmgr.getselectedobjectcount()
if selcount > 0 then
for i = 1 to swselmgr.getselectedobjectcount()
set swselobj = swselmgr.getselectedobject5(i)
set swcomp = swselmgr.getselectedobjectscomponent2(i)
set swcompmodel = swcomp.getmodeldoc
dim partdoc as sldworks.partdoc
set partdoc = swcomp.getmodeldoc
retvar_old = partdoc.materialpropertyvalues
'cambiare dicitura materiale a seconda dell'esigenza, se serve anche il percorso della libreria
partdoc.setmaterialpropertyname2 "default", "c:/program files/solidworks corp/solidworks/lang/italian/sldmaterials/solidworks din materials.sldmat", "1.5714 (16nicr4)"
'nel caso non si volesse forzare il colore del materiale commentare il codice fino a "fine commento"
'questa parte associa il colore rosso alla parte indifferentemente dal colore del materiale impostato nella scheda materiale si solidworks
vmatprop = swcomp.materialpropertyvalues
if isempty(vmatprop) then
if swcompmodel is nothing then
exit sub
end if
vmatprop = swcompmodel.materialpropertyvalues
end if
retvar = partdoc.materialpropertyvalues
retvar(0) = retvar_old(0)
retvar(1) = retvar_old(1)
retvar(2) = retvar_old(2)
retvar(3) = retvar_old(3)
retvar(4) = retvar_old(4)
retvar(5) = retvar_old(5)
retvar(6) = retvar_old(6)
retvar(7) = retvar_old(7)
retvar(8) = retvar_old(8)
partdoc.materialpropertyvalues = retvar
'"fine commento"
partdoc.save2 true
next
end if
swmodel.clearselection2 true
end if

end sub
 
eh yes, I tried several times, changes the material in "1.5714 (16nicr4)" and does not keep the color I set manually. Maybe that's the hippo? the color I have is not the std color of the material but a random color I admit.
 
prova adesso, in effetti poi non ha funzionato neanche a me!o_o

dim swapp as sldworks.sldworks
dim modeldoc2 as sldworks.modeldoc2
dim swcompmodel as sldworks.modeldoc2
dim swmodel as sldworks.modeldoc2
dim swselmgr as sldworks.selectionmgr
dim swcomp as sldworks.component2
dim vmatprop as variant
dim bret as boolean
dim longstatus as long, longwarnings as long
global const swdocpart = 1
global const swdocasm = 2
dim retvar as variant
dim retvar_old as variant
sub main()
set swapp = createobject("sldworks.application")
set modeldoc2 = swapp.activedoc
if modeldoc2 is nothing then
msgbox "nessun documento caricato." + chr$(10) + chr$(13) _
+ "carica un file di parte o assieme."
exit sub
end if
filetyp = modeldoc2.gettype
if filetyp = swdocpart then
modeldoc2.materialpropertyvalues = retvar
modeldoc2.editrebuild3
end if
if filetyp = swdocasm then
set swmodel = swapp.activedoc
dim i as integer
dim selcount as integer
set swselmgr = swmodel.selectionmanager
selcount = swselmgr.getselectedobjectcount()
if selcount > 0 then
for i = 1 to swselmgr.getselectedobjectcount()
set swselobj = swselmgr.getselectedobject5(i)
set swcomp = swselmgr.getselectedobjectscomponent2(i)
set swcompmodel = swcomp.getmodeldoc
dim partdoc as sldworks.partdoc
set partdoc = swcompmodel

retvar_old = partdoc.materialpropertyvalues
'
'cambiare dicitura materiale a seconda dell'esigenza, se serve anche il percorso della libreria
partdoc.setmaterialpropertyname2 "default", "c:/program files/solidworks corp/solidworks/lang/italian/sldmaterials/solidworks din materials.sldmat", "1.5714 (16nicr4)"

'nel caso non si volesse forzare il colore del materiale commentare il codice fino a "fine commento"
'questa parte associa il colore rosso alla parte indifferentemente dal colore del materiale impostato nella scheda materiale si solidworks
vmatprop = partdoc.materialpropertyvalues
if isempty(vmatprop) then
if swcompmodel is nothing then
exit sub
end if
'vmatprop = swcompmodel.materialpropertyvalues
end if
retvar = partdoc.materialpropertyvalues
retvar(0) = retvar_old(0)
retvar(1) = retvar_old(1)
retvar(2) = retvar_old(2)
retvar(3) = 1
retvar(4) = 1
retvar(5) = 1
retvar(6) = 0.31
retvar(7) = 0
retvar(8) = 0
partdoc.materialpropertyvalues = retvar
'"fine commento"
partdoc.save2 true
next
end if
swmodel.clearselection2 true
end if
end sub
 
I had tried with a set with two components, one of these was without material and the part was colored red (the colored part, the body and the faces did not have color, otherwise the external color is commanded by faces and body).
I had pre-selected the face of a component and launched the macro, the component changed material and keeping the red color.
Don't you?
 
I'm sorry if I answer you now.

I already have the materials and colors assigned by me. change the material and put the default color of the material.
 
Sorry I don't want to misunderstand.
you want the material to be changed but not the color, it has to keep that before the material change, right?
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top