Cancel
Showing results for 
Search instead for 
Did you mean: 

Part Color in Assembly in C#

Pioneer
Pioneer

Can someone please point me in the direction of changing part (body) colors either before or after they are moved to the assembly?

 

I have found numerous posts, most in vb but they do not work in my code. I have tried facestyle, etc. but I keep getting an error stating that I am trying to convert an object into a facestyle (first line below)...

                    facestyle = occurrence.FaceStyle;                    facestyle.SetDiffuse(0, 0.9f, 0);
                    facestyle.SetAmbient(0, 0.4f, 0);
                    facestyle.SetEmission(0, 0.1f, 0);

It would be ideal if I can do it in the assembly function, calling occurences and then changing their colors in C#.

 

Please advise.

8 REPLIES 8

Re: Part Color in Assembly in C#

Pioneer
Pioneer

Never mind ... figured it out.

 

SolidEdgeFramework.FaceStyles faceStyles = (FaceStyles)sheetMetalDocument.FaceStyles;
SolidEdgeFramework.FaceStyle faceStyle = (SolidEdgeFramework.FaceStyle)faceStyles.Item("White");
sheetMetalDocument.SetBaseStyle(PartBaseStylesConstants.sePartBaseStyle, faceStyle);

All that for 3 lines of code...  but it works.

 

Re: Part Color in Assembly in C#

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Normally, there are 3 options to change the face style of an occurrence:

  1. Directly on the occurrence
  2. In trhe body of the part occurrence
  3. Reset the face style to "(None)"

If you run the following code on the "Carrier.asm" from the training folder and debug it line-by-line, you will see the effect of the 3 options:

AssemblyDocument doc = app.ActiveDocument as AssemblyDocument;
FaceStyles asmStyles = doc.FaceStyles as FaceStyles;
Occurrence occ = doc.Occurrences.Item(1);

// (Option 1) Set the face style directly in the occurrence
occ.FaceStyle = asmStyles.Item("Blue");

// (Option 2) Set the face style in the part
occ.PutStyleUsePartStyle();
PartDocument prt = occ.OccurrenceDocument as PartDocument;
FaceStyles styles = prt.FaceStyles as FaceStyles;
Body bod = prt.Models.Item(1).Body as Body;
Faces faces = bod.Faces[FeatureTopologyQueryTypeConstants.igQueryAll] as Faces;
Array arr = Array.CreateInstance(typeof(Object), 1);
arr.SetValue(faces as Object, 0);
bod.SetFacesStyle(1, arr, styles.Item("Red") as FaceStyle);

// (Option 3) Reset the face style to (None)
occ.PutStyleNone();
// We also need to reset the style in the part
bod.SetFacesStyle(1, arr, null);
// Update the view to reflect the reset styles
(app.ActiveWindow as Window).View.Update();

Re: Part Color in Assembly in C#

Pioneer
Pioneer

Thanks MartinBernhard, always appreciated.

 

 

Re: Part Color in Assembly in C#

Valued Contributor
Valued Contributor

Martin,

I am trying to run your code but i am missing something. I would guess a using statment. My coding skill are not even at the beginner level . Could you tell me what i am missing? I figured out the SE using statments on my own Smiley Happy

 

Capture.PNG

 

 

 

Nathan M. Gardner

https://www.etsy.com/people/NathanMGardner

Be sure to check out my work!!
Made in Maine Wood Carvings

Re: Part Color in Assembly in C#

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Nathan,

you will always need to connect to or start an instance of Solid Edge first.

This part, along with the using/import instructions, I normally omit in my code snippets for simplicity Smiley Frustrated

I rarely post entire solutions Smiley Sad

Here is a macro template, you may consider for C#, but you will still need to add the OleMessageFilter class to your project. You can copy the code for it from the SE SDK Online help.

 

using System.Runtime.InteropServices;

namespace SolidEdge.Training
{
   class Program
   {
      [STAThread]
      static void Main(string[] args)
      {
         SolidEdgeFramework.Application app = null;
         try
         {
            OleMessageFilter.Register();
            ' Connect to a running instance of Solid Edge.
            app = (SolidEdgeFramework.Application) Marshal.GetActiveObject(“SolidEdge.Application”);
            ' Put your code here
         }
         catch (System.Exception ex)
         {
            Console.WriteLine(ex.Message);
         }
         finally
         {
            OleMessageFilter.Unregister();
         }
      }
   }
}

 

Re: Part Color in Assembly in C#

Valued Contributor
Valued Contributor
oh thank you Martin, while i have your ear and you suggest how i might learn to code in C# for SE? I have only taken some online beginner coarse from Lynda.com and Udemy. Should i be looking at real brick and mortar classes? anything you suggest would be great.
Nathan M. Gardner

https://www.etsy.com/people/NathanMGardner

Be sure to check out my work!!
Made in Maine Wood Carvings

Re: Part Color in Assembly in C#

Valued Contributor
Valued Contributor
I meant,
while i have your ear, can you suggest ......
Nathan M. Gardner

https://www.etsy.com/people/NathanMGardner

Be sure to check out my work!!
Made in Maine Wood Carvings

Re: Part Color in Assembly in C#

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Please check out the VERY GOOD Solid Edge SDK Samples from Jason Newell.