Community
PowerShape and PowerMill API Forum
Welcome to Autodesk’s PowerShape and PowerMill API Forums. Share your knowledge, ask questions, and explore popular API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Differentiate between PSSolid and PSSurface

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
nbaranowski
719 Views, 5 Replies

Differentiate between PSSolid and PSSurface

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

Tags (2)
5 REPLIES 5
Message 2 of 6

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;
  }
}

Luke Edwards
Consulting Services Manager
Message 3 of 6

That definitely looks like what i need thank you! Although i am getting a NullReferenceException on the line "surface.Material.ColourR = R;". 

 

 

Message 4 of 6

What is it that is null? Is it the surface or the Material?

________________________________

Autodesk Limited
Registered Office: One Discovery Place, Columbus Drive, Farnborough, Hampshire GU14 0NZ
Registered in England and Wales, No. 1839239

Luke Edwards
Consulting Services Manager
Message 5 of 6

Material is the null

Message 6 of 6
nbaranowski
in reply to: nbaranowski

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();
            }


}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report