4 Replies Latest reply on Jul 14, 2016 8:48 AM by Mark Schwandt

    Linking sketch text to custom property

    Mark Schwandt

      I've written a macro that creates a little placeholder item for parts that go on our BOMs but aren't real items. I can get it to make the part, add the text, and change the sketch text font parameters. The one thing I can't seem to get to work is setting the text to a file property link within the macro. It always wants to fill in the value of the custom property to the sketch text, not create the link. I've tried setting the name of the link to a string, (LName = "$PRP:" & CHR(34) & "Drawing No."&CHR(34)) . Then I add the the sketch to a face to extrude it:

       

      (face already selected in macro)

       

      ' Add the Drawing No. text to the face

       

      boolstatus = swExt.SelectByID2("", "FACE", -5.2647223348572E-03, -7.04248185229517E-04, 2.53999999756616E-04, False, 0, Nothing, 0)

      Set vSkLines = swSketchMgr.CreateCenterLine(-0.01905, -0.003081, 0#, 0.01905, -0.003081, 0#)

      boolstatus = swExt.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)

      boolstatus = swExt.SelectByID2("Line1", "SKETCHSEGMENT", 1.82047161818376E-04, -2.96876910042268E-03, 1.27000000000021E-04, True, 0, Nothing, 0)

      Set Annotation = swModel.AddDimension2(2.07113655637978E-02, -1.5123918058757E-03, 0.000254)

      swModel.ClearSelection2 True

      Set myDimension = swModel.Parameter("D1@Sketch2")

      myDimension.SystemValue = 0.003175

       

      boolstatus = swExt.SelectByID2("Line1", "SKETCHSEGMENT", -8.54221297763129E-04, -3.08079812308014E-03, 1.27000000000022E-04, False, 1, Nothing, 0)

      Set vSkLines = swModel.InsertSketchText(0, 0, 0, LName, 1, 0, 0, 80, 100)

      swModel.ClearSelection2 True

       

      ' Here I change the default font and text height

       

      boolstatus = swExt.SelectByID2("SketchText1", "SKETCHTEXT", 0, 0, 0, False, 0, Nothing, 0)

      Set SelMgr = swModel.SelectionManager

      Set swSketchText = SelMgr.GetSelectedObject6(1, 0)

      Set swTextFmt = swSketchText.GetTextFormat

       

      swTextFmt.CharHeight = 0.00635

      swTextFmt.TypeFaceName = "Arial"

       

      'swModel.InsertSketch2 True

      boolstatus = swSketchText.SetTextFormat(False, swTextFmt)

       

      ' Extrude the Text

       

      Set swFeature = swModel.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.000254, 0.000254, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)

       

      But no matter what I try, It defaults by putting in the value of the custom property "Drawing No." and not the link.

       

      Any suggestions?

        • Re: Linking sketch text to custom property
          Peter Brinkhuis

          If I understand your question correctly you want to assign the value $PRP:"Drawing No." to the text on the part, and you want to keep the link so the part updates when the custom property updates? I've tried to add this manually and got it to work.

           

          Can you post the rest of the code that might contain the most useful parts? I think you are trying to do too many things at once here. You want the whole snippet to work, but a basic part doesn't function yet. Can you create a tiny section of code that just assigns the value and evaluate the value of LName using debug.print?

           

          This works for me, I have a custom property called test of type text and it contains some text. I am in sketch mode when I run the macro. The value in the sketch gets updated after I edit the custom property, the sketch text contains $PRP:"test"

           

          Sub main()

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim value As SketchText

          Dim customprop As String

           

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

           

          customprop = "$PRP:" & Chr(34) & "test" & Chr(34)

           

          Debug.Print customprop

          Set value = swModel.InsertSketchText(0, 0, 0, customprop, 0, 0, 0, 100, 100)

           

          End Sub

          • Re: Linking sketch text to custom property
            Deepak Gupta

            If I use these codes then it works fine.

             

            Option Explicit   

                Dim swApp As SldWorks.SldWorks

                Dim swModel As SldWorks.ModelDoc2

                Dim swSketchText As SldWorks.SketchText   

            Sub main()

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc   

                Set swSketchText = swModel.InsertSketchText(0, 0, 0, "$PRP:" & Chr(34) & "Drawing No." & Chr(34), 0, 0, 0, 100, 100)

                swModel.InsertSketch2 True   

            End Sub

            In your codes if I remove the line after you've inserted the sketch text and end the sketch then it works fine

             

             

            So may be you need to end the sketch first and then reelect it , edit and make changes. You may also report this to your VAR.

              • Re: Linking sketch text to custom property
                Mark Schwandt

                My problem seems to be that I need to change the font and font size. If I don't change them, everything is fine, but the minute I change the font and size, the link breaks and it substitutes the value rather than the property.

                 

                I'm going to see if I try to insert the text again after the font change, will it stay linked.

                • Re: Linking sketch text to custom property
                  Mark Schwandt

                  It turns out that if I add one line following:

                  boolstatus = swSketchText.SetTextFormat(False, swTextFmt)

                   

                  that reads:

                  swSketchText.Text = LName

                   

                  and then extrude the text, I get the corrected text font and size, and it retains the property link.

                  I can even put a dummy value in the original swModel.InsertSketchText line, and it will act as a placeholder until I add the link value.

                   

                  It seems to be a glitch that if you try to edit the font after you insert the text that has a link it replaces the link property with the value from the link.

                  I've tried it several ways and it seems to always do this. So it seems you need to change the text back to the link value after you make any changes to the text format.