I need to grab everything on a level and color the objects on that level. The problem i am having it when there is a mix of solids and surfaces on the level. I get an exception because the script is trying to cast a PSSurface to PSSolid. Should i be using PSEntity in some way instead of this method?
public void ColorCheck(PSLevel level, byte R, byte G, byte B) { PSModel psModel = powerSHAPE.ActiveModel; psModel.ClearSelectedItems(); psModel.Levels.DeactivateAllLevels(); // Turn Data On level.IsActive = true; psModel.SelectAll(); try { foreach (PSSolid sol in psModel.SelectedItems) { sol.Material.ColourR = R; sol.Material.ColourG = G; sol.Material.ColourB = B; } foreach (PSSurface surf in psModel.SelectedItems) { surf.Material.ColourR = R; surf.Material.ColourG = G; surf.Material.ColourB = B; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); resetGUI(); } }
Essentially, no matter what is on level "level" in this script, i need to change the color of all of it.
Thank you,
-Nick
Solved! Go to Solution.
Solved by luke.edwards.autodesk. Go to Solution.
I think the solution you need is something like:
foreach (PSEntity entity in psModel.SelectedItems) { if (entity is PSSurface) { var surface = entity as PSSurface; surface.Material.ColourR = R; surface.Material.ColourG = G; surface.Material.ColourB = B; } else if (entity is PSSolid) { var solid= entity as PSSolid; solid.Material.ColourR = R; solid.Material.ColourG = G; solid.Material.ColourB = B; } }
That definitely looks like what i need thank you! Although i am getting a NullReferenceException on the line "surface.Material.ColourR = R;".
I was able to get it to work by creating a material first and then using it in the loops. However it takes forever to iterate through surfaces. Thank you very much for your time and leading me to the solution!
public void ColorCheck(PSLevel level, byte R, byte G, byte B) { PSModel psModel = powerSHAPE.ActiveModel; psModel.ClearSelectedItems(); psModel.Levels.DeactivateAllLevels(); // Turn Data On level.IsActive = true; psModel.SelectAll(); try { //Create materials if they don't exist PSMaterial color = psModel.Materials.CreateMaterial("Overlaid Red", 0.5, 0.5, 0.0, 0.6, R, G, B); foreach (PSEntity entity in psModel.SelectedItems) { if (entity is PSSurface) { var surface = entity as PSSurface; surface.Material = color; } else if (entity is PSSolid) { var solid = entity as PSSolid; solid.Material = color; } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); resetGUI(); } }