3 Replies Latest reply on Dec 3, 2014 12:05 PM by Ash Carlton

    Sketch name creation

    Ash Carlton

      I'm trying to use the macro listed under the help files to create a bounding box within an assembly. Please see the link below.

       

      I'm able to get that to work, but it could be better. Is there a way for the macro to name the 3DSketch the same name every time? Like "Bounding Box" instead of "3DSketch1"

       

      http://help.solidworks.com/2014/English/api/sldworksapi/Get_Assembly_Bounding_Box_using_Assembly_Example_VB.htm

        • Re: Sketch name creation
          Matt Martens

          You need another variable: swFeat as SldWorks.Feature

           

          then use

           

          swFeat = swSketchMgr.ActiveSketch

          swFeat.Name = "Bounding Box"

           

          anytime during the macro the sketch is in edit mode (between the 2 lines of "swSketchMgr.Insert3DSketch True").

          • Re: Sketch name creation
            Alice Caspari

            Ash,

            This is the code I modified probably from the same file. It renames the sketch and also is adding some dimensions to be extracted in the drawing

             

            ' Adds a 3D sketch to the assembly showing the dimensioned bounding box.

            Option Explicit

            Sub main()
                Dim swApp                   As SldWorks.SldWorks
                Dim swModel                 As SldWorks.ModelDoc2
                Dim swAssy                  As SldWorks.AssemblyDoc
                Dim vBox                    As Variant
                Dim X_max                   As Double
                Dim X_min                   As Double
                Dim Y_max                   As Double
                Dim Y_min                   As Double
                Dim Z_max                   As Double
                Dim Z_min                   As Double
                Dim swSketchMgr             As SldWorks.SketchManager
                Dim swSketchPt(9)           As SldWorks.SketchPoint
                Dim swSketchSeg(14)         As SldWorks.SketchSegment
                Dim swSketch                As SldWorks.Sketch
                Dim swFeat                  As SldWorks.Feature
                 
               
                Set swApp = CreateObject("SldWorks.Application")
                Set swModel = swApp.ActiveDoc
                Set swAssy = swModel
               
                vBox = swAssy.GetBox(0)
                  
                ' Initialize values
                X_max = vBox(3)
                X_min = vBox(0)
                Y_max = vBox(4)
                Y_min = vBox(1)
                Z_max = vBox(5)
                Z_min = vBox(2)
               
                ' Turn off Dimension Dialogs
                swApp.SetUserPreferenceToggle swInputDimValOnCreate, False
               
                Set swSketchMgr = swModel.SketchManager
                swSketchMgr.Insert3DSketch True
                swSketchMgr.AddToDB = True
               
                ' Draw points at each corner of bounding box
                Set swSketchPt(0) = swSketchMgr.CreatePoint(X_min, Y_min, Z_min)
                Set swSketchPt(1) = swSketchMgr.CreatePoint(X_min, Y_min, Z_max)
                Set swSketchPt(2) = swSketchMgr.CreatePoint(X_min, Y_max, Z_min)
                Set swSketchPt(3) = swSketchMgr.CreatePoint(X_min, Y_max, Z_max)
                Set swSketchPt(4) = swSketchMgr.CreatePoint(X_max, Y_min, Z_min)
                Set swSketchPt(5) = swSketchMgr.CreatePoint(X_max, Y_min, Z_max)
                Set swSketchPt(6) = swSketchMgr.CreatePoint(X_max, Y_max, Z_min)
                Set swSketchPt(7) = swSketchMgr.CreatePoint(X_max, Y_max, Z_max)
                Set swSketchPt(8) = swSketchMgr.CreatePoint(X_min, Y_min, 0)

                ' Draw bounding box, Add X and Y Dimensions
                Set swSketchSeg(0) = swSketchMgr.CreateLine(X_min, Y_min, Z_min, X_max, Y_min, Z_min)
                Set swSketchSeg(1) = swSketchMgr.CreateLine(X_max, Y_min, Z_min, X_max, Y_min, Z_max)
                'Set model Normal to Right Plane for Z Dimension Placement
                swModel.ShowNamedView2 "*Front", 1
                Set swSketchSeg(2) = swSketchMgr.CreateLine(X_max, Y_min, Z_max, X_min, Y_min, Z_max)
                ' Length Dimension
                swModel.AddDimension2 0, -0.075 - Y_max / 2, 0
                'Set model Normal to Right Plane for X and Y Dimension Placement
                swModel.ShowNamedView2 "*Left", 3
                Set swSketchSeg(3) = swSketchMgr.CreateLine(X_min, Y_min, Z_max, X_min, Y_min, Z_min)
                ' Width Dimension
                swModel.AddDimension2 0, -0.05 - X_max / 2, 0
                Set swSketchSeg(4) = swSketchMgr.CreateLine(X_min, Y_min, Z_min, X_min, Y_max, Z_min)
                ' Height Dimension
                swModel.AddDimension2 0, 0, -0.05 - X_max / 2
                Set swSketchSeg(5) = swSketchMgr.CreateLine(X_min, Y_min, Z_max, X_min, Y_max, Z_max)
                Set swSketchSeg(6) = swSketchMgr.CreateLine(X_max, Y_min, Z_min, X_max, Y_max, Z_min)
                Set swSketchSeg(7) = swSketchMgr.CreateLine(X_max, Y_min, Z_max, X_max, Y_max, Z_max)
                Set swSketchSeg(8) = swSketchMgr.CreateLine(X_min, Y_max, Z_min, X_max, Y_max, Z_min)
                Set swSketchSeg(9) = swSketchMgr.CreateLine(X_max, Y_max, Z_min, X_max, Y_max, Z_max)
                Set swSketchSeg(10) = swSketchMgr.CreateLine(X_max, Y_max, Z_max, X_min, Y_max, Z_max)
                Set swSketchSeg(11) = swSketchMgr.CreateLine(X_min, Y_max, Z_max, X_min, Y_max, Z_min)
                'Add Sketch Lines and Dimensions for "From Center" Dimensions
                Set swSketchSeg(12) = swSketchMgr.CreateLine(X_min, Y_min, 0, X_min, Y_min, Z_min)
                ' Width Dimension
                swModel.AddDimension2 0, -0.01 - X_max / 2, 0
                Set swSketchSeg(13) = swSketchMgr.CreateLine(X_min, Y_min, 0, X_min, Y_min, Z_max)
                ' Width Dimension
                swModel.AddDimension2 0, -0.01 - X_max / 2, 0
                swSketchMgr.AddToDB = False
             
                'Rename the sketch
                Set swSketch = swModel.GetActiveSketch2
                Set swFeat = swSketch
                swFeat.Name = "BoundingBoxSketch"
                swSketchMgr.Insert3DSketch True
               
               
            End Sub

            1 person found this helpful