10 Replies Latest reply on May 10, 2018 9:25 AM by Jacob Corder

    SolidWorks API C# Body Transparency

    Vadim Vadim

      Hello, guys. I'm working with SolidWorks plug-in. I need to create transparent body, but I can't do it. Can someone help me ??
      I create new body by IModeler class, like this:

       

              protected virtual IBody2[] OnCreateBodies(ISldWokrs application, IModelDoc2 document, DetectorMacroFeatureData properties)
              {
                  double halfWidthX = (double)properties["X Half Width"].Value;
                  double halfWidthY = (double)properties["Y Half Width"].Value;
                  IModeler modeler = application.IGetModeler();
                  IPartDoc part = document as IPartDoc;
                  Point3D[] points = new Point3D[] { new Point3D(-halfWidthX, -halfWidthY, 0.5), new Point3D(-halfWidthX, halfWidthY, 0.5),
                                                     new Point3D(halfWidthX, halfWidthY, 0.5), new Point3D(halfWidthX, -halfWidthY, 0.5) };
                  Curve[] border = new Curve[4];
      
      
                  for (int i = 0; i < 4; i++)
                  {
                      border[i] = SWGeometry.CreateLine(application, points[i], points[(i + 1) % 4]);
                  }
      
      
                  Surface planeSurf = modeler.CreatePlanarSurface2(new double[] { 0, 0, 0.5 }, new double[] { 0, 0, 1 }, new double[] { 1, 0, 0 }) as Surface;
                  IBody2 detectorBody = planeSurf.CreateTrimmedSheet4(border, true) as IBody2;         
      
      
                  return new IBody2[] { detectorBody };
              }
      

       

      When i use this function, i create correct planar body. Now i need to do it invisible (not hiden). So i want to set transparent as 100 %.
      I reed, that i can do it by using MaterialPropertyValues2 property. But it is not work for me. When i try to get or set it, it always equals NULL.

       

      var material = detectorBody.MaterialPropertyValues2 as double[]; // return NULL, like double[] or like object, anyway it equals NULL
      material[7] = 1.0;
      detectorBody.MaterialPropertyValues2 = material; // still equals NULL
      

       

      Can someone help me or show some offers ?? I'm begginer in work with SolidWorks API, so it will be nice, if you show me some examples.

        • Re: SolidWorks API C# Body Transparency
          Carlos Almeida

          You should call Body.Display3() before using Body.MaterialPropertyValues2.

          • Re: SolidWorks API C# Body Transparency
            Jacob Corder

            As Carlos said you do need to call ibody2::Display3 first. Otherwise the body does not have material property values associated with it.

             

            Then you can set the colors. Colo is (1/255) for rgb so rgb 255,255,255 is 1,1,1 to sw.

             

            Just so you know, you won't be able to set the visibility of it to transparent, at least I haven't found a way, or I stopped trying. I can't remember.

             

            One way to do this is to set the default part appearance to transparent. This will then make your temp body transparent.

              • Re: SolidWorks API C# Body Transparency
                Emerson Bottero

                MaterialPropertyValues2 is bugged in temporary bodies

                When I set it to 0.75, 0.75, 0,1,1,0.5,0.4,0.8,0.21 (I wanted a Transparent Yellow)

                and read the values in the next line the result is

                0,0.75,0,1,1,0,2.47,0,0 (green not transparent)

                 

                Couldn't find a workaround it.

                  • Re: SolidWorks API C# Body Transparency
                    Jacob Corder
                     Public Function ColorVisibleBodyWithMaterialProperties(ByRef BodyToColor As Body2, ByVal BodyColor As System.Drawing.Color, Optional ByVal Ambient As Double = 1, Optional ByVal Diffuse As Double = 1,
                                                            Optional ByVal Specular As Double = 0, Optional ByVal Shininess As Double = 0, Optional ByVal Transparency As Double = 0, Optional ByVal Emissions As Double = 0) As Boolean
                            ColorVisibleBodyWithMaterialProperties = False
                            If IsNothing(BodyToColor) = False Then
                                Try
                                    If BodyToColor.Visible = True Then
                                        If BodyToColor.HasMaterialPropertyValues = True Then
                                            Dim MatProps(8) As Double
                                            Dim CurrentProps As Object = BodyToColor.MaterialPropertyValues2
                                            MatProps(0) = Convert.ToDouble(BodyColor.R) / 255 'R
                                            MatProps(1) = Convert.ToDouble(BodyColor.G) / 255 'G
                                            MatProps(2) = Convert.ToDouble(BodyColor.B) / 255 'B
                                            MatProps(3) = Ambient 'Ambient
                                            MatProps(4) = Diffuse 'Diffuse 
                                            MatProps(5) = Specular 'Specular
                                            MatProps(6) = Shininess 'Shininess
                                            MatProps(7) = Transparency 'Transparency
                                            MatProps(8) = Emissions 'Emissions
                                            Try
                                                For i = 0 To 8
                                                    If Convert.ToDouble(CurrentProps(i)) <> MatProps(i) Then
                                                        Try
                                                            BodyToColor.MaterialPropertyValues2 = MatProps
                                                            MatProps = Nothing
                                                        Catch ex As Exception
                    
                    
                                                        End Try
                    
                    
                                                        Return True
                                                    End If
                                                    If i = 8 Then Return True
                                                Next
                                            Catch ex As Exception
                                                Try
                                                    BodyToColor.MaterialPropertyValues2 = MatProps
                                                    MatProps = Nothing
                                                Catch ex1 As Exception
                    
                    
                                                End Try
                                            End Try
                                            CurrentProps = Nothing
                                            MatProps = Nothing
                                        Else
                                            Return False
                                        End If
                                    Else
                                        Return False
                                    End If
                                Catch ex As Exception
                    
                    
                                End Try
                            End If
                        End Function
                    

                     

                     

                    i have no problem with it.

                     

                    try this.

                      • Re: SolidWorks API C# Body Transparency
                        Carlos Almeida

                        This code is just a little bit awkward!

                          • Re: SolidWorks API C# Body Transparency
                            Jacob Corder

                            it is written for efficiency as calls through the COM boundary are expensive. 

                            it is cheaper to check if double values are equal than it is to just assign them to a com object.

                             

                            but i will make sure i don't post any code if you need any, as it may be awkward!

                              • Re: SolidWorks API C# Body Transparency
                                Carlos Almeida

                                Jacob,

                                 

                                Sorry for any misunderstanding! It was not my point to hurt your feelings.

                                I wasn't getting the point of all that code for this simple purpose. Thanks for your explanation.

                                 

                                By the way, do you know if the use of the "Try Catch" loop has any substantial performance impact?

                                I always try to avoid having too much of these loops!

                                  • Re: SolidWorks API C# Body Transparency
                                    Jacob Corder

                                    no hurt feelings here bud.

                                     

                                    loops to check if values are the same execute in nearly no time.

                                     

                                    try catch finally is something that you should ALWAYS ALWAYS ALWAYS do in code, especially in COM Interop.

                                    if you dont then your application has a high probability of crashing when deployed.

                                     

                                    try catch finally does not induce a performance issue unless an error is trapped, then it does have a delay but instead of crashing the system, it doesnt. 

                                     

                                    i have 5 years of writing solidworks API exclusively at work and from my experience, you can never trust what solidworks sends you. nor should you ever trust that values in methods are actually valid. even if they are your own methods.

                                     

                                    better safe than sorry in my opinion

                                • Re: SolidWorks API C# Body Transparency
                                  Jacob Corder
                                  Public Function ColorVisibleBodyWithMaterialProperties(ByRef BodyToColor As Body2, ByVal BodyColor As System.Drawing.Color, Optional ByVal Ambient As Double = 1, Optional ByVal Diffuse As Double = 1,
                                                                          Optional ByVal Specular As Double = 0, Optional ByVal Shininess As Double = 0, Optional ByVal Transparency As Double = 0, Optional ByVal Emissions As Double = 0) As Boolean
                                          If BodyToColor Is Nothing Then Return False
                                          Try
                                              If BodyToColor.Visible = False Then Return False
                                              If BodyToColor.HasMaterialPropertyValues = False Then Return False
                                              Dim MatProps(8) As Double
                                              Dim CurrentProps As Object = BodyToColor.MaterialPropertyValues2
                                              MatProps(0) = Convert.ToDouble(BodyColor.R) / 255 'R
                                              MatProps(1) = Convert.ToDouble(BodyColor.G) / 255 'G
                                              MatProps(2) = Convert.ToDouble(BodyColor.B) / 255 'B
                                              MatProps(3) = Ambient 'Ambient
                                              MatProps(4) = Diffuse 'Diffuse 
                                              MatProps(5) = Specular 'Specular
                                              MatProps(6) = Shininess 'Shininess
                                              MatProps(7) = Transparency 'Transparency
                                              MatProps(8) = Emissions 'Emissions
                                              Try
                                                  For i = 0 To 8
                                                      If Convert.ToDouble(CurrentProps(i)) <> MatProps(i) Then
                                                          Try
                                                              BodyToColor.MaterialPropertyValues2 = MatProps
                                                              Return True
                                                          Catch ex As Exception
                                  
                                  
                                                          End Try
                                                          Return False
                                                      End If
                                                      If i = 8 Then Return True
                                                  Next
                                              Catch ex As Exception
                                                  Try
                                                      BodyToColor.MaterialPropertyValues2 = MatProps
                                                      Return True
                                                  Catch ex1 As Exception
                                  
                                  
                                                  End Try
                                              End Try
                                          Catch ex As Exception
                                  
                                  
                                          End Try
                                      End Function
                                  

                                   

                                  there.

                                   

                                  is that better Carlos?

                              • Re: SolidWorks API C# Body Transparency
                                Carlos Almeida

                                It worked for me with the same input, in a simple test that I did!