Materials again
-
me again...
i don't know why, but the attribut "ablauf", "material" always receives a new value, even if the face's material is included in the array called invalid_material. i'll post the code, because i realy don't know how to fix it.
ents.each{|e| if e.typename == "Face" start = e.get_attribute "ablauf", "baubeginn" ende = e.get_attribute "ablauf", "bauende" if (e.material) invalid_materials = ['bau_mat', 'fertig_mat', 'inplanung'] unless invalid_materials.include?( e.material.name ) mat_vorher = e.material.name e.set_attribute("ablauf", "material", mat_vorher) end end if @woche.to_i >= start.to_i && @woche.to_i < ende.to_i e.material = bau_mat end if @woche.to_i >= ende.to_i e.material =fertig_mat end if @woche.to_i < start.to_i e.material = inplanung end end } #now the code that reloads the material def mat_back mod = Sketchup.active_model ents = mod.entities mats = mod.materials default_mat = mats.add "standardwert" default_mat.color = [50,250,50] ents.each{|e| if e.typename == "Face" if (e.get_attribute "ablauf", "material") mat_temp=e.get_attribute "ablauf", "material" e.material=mats[mat_temp] else e.material=default_mat end end } end
-
To check the materials' name make the line
p mat_vorher = e.material.name
Then it will print the name of the material in the [opened] Ruby Console to compare with yourinvalid_materials
list...
Looks like they don't match ? -
it was my mistake. the materials were declared in a tool as a local variable. every time the tool was invoken, the materials werde declared. so to the materials name was a number added, because the material already existed in the materials container. when you only once invoke the tool everything is fine. one more an it didn't work.
is it possible to declare a material as a constant or to declare it with an "@" in a class to avoid that?
-
Why not just check if the material exists?
-
For example:
if mats["standardwert"] default_mat = mats["standardwert"] else default_mat = mats.add("standardwert") default_mat.color = [50,250,50] end
-
it works. thanks a lot, once more
tommorow t'll try to declare the material in the constructor of the class anyway, to avoid putting all the trash in the memory.
bye.
tim
Advertisement