3 Replies Latest reply on Feb 2, 2018 10:57 PM by Rob Edwards

    sketch picture macro

    Liam Mooney

      I regularly create a sketch in the top plane and insert a sketch picture, but the location of the sketch picture is different every time. Is there a way to run a macro that takes me to the Open dialogue so I can navigate to the image myself?

       

      This macro inserts the same image every time.

       

      Dim swApp As Object

       

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

       

      Sub main()

       

       

      Set swApp = _

      Application.SldWorks

       

       

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)

      Part.SketchManager.InsertSketch True

      Dim SkPicture As Object

      Set SkPicture = Part.SketchManager.InsertSketchPicture("File Location")

      End Sub

       

       

      What can I change so it lets me choose what image to open?

      Thank you

          • Re: sketch picture macro
            Liam Mooney

            Thanks I was able to get that to work!

             

            The images I import are generally very large, and then I scale them down.

             

            I tried this:

            Dim SkPicture As Object

            Set SkPicture = Part.SketchManager.InsertSketchPicture(fileName)

            SkPicture.SetSize 0.1, 0.1, 1

             

            But it changes the aspect ratio to 1:1.  Do you know how I can resize the image by only specifying one height and locking aspect ratio?

              • Re: sketch picture macro
                Rob Edwards

                Hi Liam

                I guess you just pass zero for the size you don't want

                eg

                .SetSize(0.1, 0#, True)

                 

                seems to work

                 

                Option Explicit
                
                Dim swApp As SldWorks.SldWorks
                Dim swModel As SldWorks.ModelDoc2
                Dim swSketchPicture As SldWorks.SketchPicture
                Dim filename As String
                Dim fileFilter As String
                
                Sub main()
                
                    Set swApp = Application.SldWorks
                   With swApp
                        Set swModel = .ActiveDoc
                        
                        fileFilter = "Picture Files(*.bmp;*.gif;*.jpg;*.tif;*.wmf;*.png;*.psd)|*.bmp;*.gif;*.jpg;*.tif;*.wmf;*.png;*.psd|" _
                                   & "Windows Bitmap Files(*.bmp)|*.bmp|" _
                                   & "Graphics Interchange Format(*.gif)|*.gif|" _
                                   & "JPEG Image Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|" _
                                   & "TIFF Files(*.tif;*.tiff)|*.tif;*.tiff|" _
                                   & "WMF Files(*.wmf)|*.wmf|" _
                                   & "Portable Network Graphics(*.png)|*.png|" _
                                   & "All Files(*.*)|*.*|"
                        
                         filename = .GetOpenFileName("Open", "", fileFilter, 0, "", "")
                   End With
                   
                   With swModel
                       .Extension.SelectByID2 "Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
                        With .SketchManager
                             .InsertSketch True
                                  Set swSketchPicture = .InsertSketchPicture(filename)
                                 Call swSketchPicture.SetSize(Width:=0#, Height:=0.2, AspectRatioLocked:=True)
                             .InsertSketch True
                        End With
                   End With
                End Sub