5 Replies Latest reply on Apr 23, 2018 12:04 PM by Douglas Saenz

    How to change color of sketch in an Assembly Document using the API

    Marcin Pilat

      Hi,  I have a question that I couldn't find an answer to after days of searching.

       

      We have an assembly document that has several sketches. It is important to distinguish these sketched by coloring them. In the GUI, this can be easily done either through the Sketch/Curve Color functionality or using the Line Format toolbar. What is the corresponding API to get the same functionality (something that works with C#)?

       

      When you record a macro of the Sketch/Curve Color functions, nothing is recorded. When you record a macro of the Line Format functions, they appear but the functions used (SetLineStyle, SetLineWidth, SetLineColor) are only defined in PartDoc or DrawingDoc. Running back the macro produces a run-time error 438 "object doesn't support this property or method".

       

      Any help is greatly appreciated!!

        • Re: How to change color of sketch in an Assembly Document using the API
          Cloud Strife Pom Cloud Strife Pom

          I have a same problem here
          try to use function like IGetMaterialPropertyValues2 to try to apply color in it but it's return -1

          • Re: How to change color of sketch in an Assembly Document using the API
            Cloud Strife Pom Cloud Strife Pom

            You can use ISketchSegment.color= "int color" but the only problem is you have to edit the sketch to see the changed color

            • Re: How to change color of sketch in an Assembly Document using the API
              Douglas Saenz

              I have the same problem and no clues on how to solve it.

                • Re: How to change color of sketch in an Assembly Document using the API
                  Simon Turner

                  This works for me:

                   

                  Option Explicit

                  Option Compare Text

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim Part As ModelDoc2

                  Dim boolstatus As Boolean

                  Dim longstatus As Long, longwarnings As Long

                   

                  Sub main()

                      Dim myFeat As Feature

                      Dim mySketch As Sketch

                      Dim mySeg As SketchSegment

                      Dim selMgr As SelectionMgr

                      Dim sa As Variant

                      Dim myConfigs As Variant

                      Dim myMgr As ConfigurationManager

                    

                      Set swApp = Application.SldWorks

                      Set Part = swApp.ActiveDoc

                      Set selMgr = Part.SelectionManager

                      myConfigs = Part.GetConfigurationNames

                    

                      'Edit the next line so that it selects one of the sketch segments in your sketch:

                      boolstatus = Part.Extension.SelectByID2("Line6@Sketch1@ComponentName-1@AssemblyName", "EXTSKETCHSEGMENT", 0#, 0#, 0#, False, 0, Nothing, 0)

                    

                      Set mySeg = selMgr.GetSelectedObject6(1, -1)

                      Set mySketch = mySeg.GetSketch

                      Set myFeat = mySketch

                      sa = Part.MaterialPropertyValues

                   

                      'Set the next 3 values to the RGB (values range between 0.0 and 1.0):

                      sa(0) = 1.0

                      sa(1) = 0.0

                      sa(2) = 0.0

                    

                      myFeat.SetMaterialPropertyValues2 sa, swInConfigurationOpts_e.swAllConfiguration, myConfigs

                      Part.ClearSelection2 True

                      Part.EditRebuild3

                      Part.GraphicsRedraw2

                  End Sub

                    • Re: How to change color of sketch in an Assembly Document using the API
                      Douglas Saenz

                      This worked for me! I appreciate your help. This is a VB.NET function I wrote to achieve the same:

                       

                              Private Sub ChangeSketchColor(ByVal swModel As ModelDoc2, ByVal sketchName As String,

                                                            ByVal red As Double, ByVal green As Double, ByVal blue As Double, ByRef status As Boolean, ByRef errorMessage As String)

                                  status = False

                                  errorMessage = ""

                       

                                  Dim swSelMgr As SelectionMgr = DirectCast(swModel.SelectionManager, SelectionMgr)

                                  Dim swModelConfigs() As String = DirectCast(swModel.GetConfigurationNames(), String())

                                  Dim boolStatus As Boolean

                       

                                  'Select the sketch

                                  boolStatus = swModel.Extension.SelectByID2(sketchName, "SKETCH", 0.0, 0.0, 0.0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)

                                  If Not boolStatus Then

                                      status = False

                                      errorMessage = "Unable to select sketch."

                                  End If

                       

                                  'Get the sketch feature

                                  Dim swFeature As Feature = DirectCast(swSelMgr.GetSelectedObject6(1, -1), Feature)

                       

                                  'Clean up

                                  swModel.ClearSelection2(True)

                       

                                  'Set the color of the sketch

                                  Dim swMaterialPropertyValues() As Double = DirectCast(swModel.MaterialPropertyValues, Double())

                                  swMaterialPropertyValues(0) = red

                                  swMaterialPropertyValues(1) = green

                                  swMaterialPropertyValues(2) = blue

                                  swFeature.SetMaterialPropertyValues2(swMaterialPropertyValues, swInConfigurationOpts_e.swAllConfiguration, swModelConfigs)

                       

                                  status = True

                              End Sub