10 Replies Latest reply on Sep 15, 2014 6:08 PM by Daniel Andersson

    Macro to do Slddrw to PNG without background?

    Flemming Nielsen

      Hi All,

       

      I need a macro to export a PNG picture of a drawing, without background and preferably in a specific DPI.

      I found a macro made by Deepak Gupta here https://forum.solidworks.com/message/385536?sr=search&searchId=9c5b419d-93e7-41af-9c5a-923a03005185&searchIndex=0#385536

      where I managed to get it saving a PNG, but still with a white background.

       

      The code is;

       

      Sub main()

       

      Dim swApp                     As SldWorks.SldWorks

      Dim swModel                   As SldWorks.ModelDoc2

      Dim swSheet                   As SldWorks.Sheet

      Dim vSheetNameArr, vSheetName As Variant

      Dim bRet                      As Boolean

      Dim lErrors                   As Long

      Dim lWarnings                 As Long

      Dim fileName                  As String

      Dim strOriginallyActiveSheet  As String

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

       

      ' Is document active?

       

      If swModel Is Nothing Then

          swApp.SendMsgToUser2 "A Drawing document must be active.", swMbWarning, swMbOk

          Exit Sub

      End If

       

      ' Is it a Drawing document?

       

      If swModel.GetType <> swDocDRAWING Then

          swApp.SendMsgToUser2 "A Drawing document must be active.", swMbWarning, swMbOk

          Exit Sub

      End If

       

      If swModel.GetTitle = "" Then

          swApp.SendMsgToUser2 "Save the Drawing first", swMbWarning, swMbOk

          Exit Sub

      End If

       

      fileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

      fileName = Left(fileName, InStrRev(fileName, ".") - 1)

       

      'Change/Set DPI Settings and Paper Size here

       

      swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swTiffPrintDPI, 300

      swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swTiffPrintPaperSize, swDwgPaperSizes_e.swDwgPaperA4size

       

      Set swSheet = swModel.GetCurrentSheet

       

      strOriginallyActiveSheet = swSheet.GetName

       

      vSheetNameArr = swModel.GetSheetNames

       

      For Each vSheetName In vSheetNameArr

       

      bRet = swModel.ActivateSheet(vSheetName): Debug.Assert bRet

       

      swModel.ViewZoomtofit2

       

      swModel.Extension.SaveAs fileName + ".PNG", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, lErrors, lWarnings

       

      Next vSheetName

       

      swModel.ActivateSheet (strOriginallyActiveSheet)

       

      End Sub

        • Re: Macro to do Slddrw to PNG without background?
          Deepak Gupta

          I don't think that you could save the image without the background. So your only option is to export is with white color background and then edit that image in an image editor like Adobe Photoshop to remove the background.

          • Re: Macro to do Slddrw to PNG without background?
            Daniel Andersson

            Hi,

            Did some google and found out that the functions within GDI+ would might be helpful.

            I'm not a software developer but it seems like that GDI+ is mostly used in C/C++. However, the libaries can still be referenced and used within VBA. But with some difference compared to the stated API references.

             

            Googled around abit more and found a .bas file for Excel that uses GDI+. I wont be able to dig further than this but... here is what I would try to do.

             

            I would set up the macro to do:

            1) Save drawing as png,jpg or tiff with SolidWorks API

            2) Open the file and use GDI+ API functions to set transparency and save it as png.

            (3) Save it to a new file and delete the old one if necessary.....)

             

            As reference, I have attached the .bas file that I found in another forum. It is written for Excel VBA. But it somehow shows that it is possible to access the GDI+ funcions with VBA.

            Futher information can be found at MSDN etc etc.

             

            You could ask at stackoverflow.com for some help seing this as a GDI+ challange... they are usually quite helpful and quick to reply.

            • Re: Macro to do Slddrw to PNG without background?
              Daniel Andersson

              Hi again Flemming

               

              I could not just let this go... I was not able or had the patient to figure out how to use  "System.Drawing" in the VBA.

              So... I made a simple exe file that sets "White" = RGB[255,255,255] to transparent of the target.


              If you like you could test this out with following set up...

              1) Use you current macro generate the PNG file

              2) Use the makeTransparent.exe [target] to make the file transparent.

              Sample of how to use makeTransparent.exe in command prompt -> makeTransparent.exe C:\test\mypngfile.png

              Capture.JPG

               

              Code to implement it to VBA:

              Dim strExe As String
              Dim strTarget As String
              strExe = "C:\makeTransparent.exe" 'Replace with your location where you put the exe file

              strTarget = "C:\test\mypngfile.png" 'Use your full pathname here for the target

              Call Shell("""" & strExe & """ """ & strTarget & """", vbHide)

              'Note that you could add quite much more functionality to this, you could add timeouts etc for the Call Shell to get some control of whats happening.


              Please note that the swiftly and most unprofissonal coded exe file does not handle any kinds of errors or exceptions. If it fails you will most like not be prompted anything...

              The exe file was complied in Visual Basic 2010 Express. Just send me a PM if you want to have the source files. I do belive that you should be able to run it without any issues, however it might fail depending on what .net Framework that is available on your machine.

               

              My support of this exe ends quite much here... Since I have hard to find time for fun things like this. But please give me feedback if try this one out.I will try to reply and discuss as much as possible.

              And of course, attached is the pre-Alpha-release of the exe.

               

              Best regards

              Daniel

              • Re: Macro to do Slddrw to PNG without background?
                Daniel Andersson

                Hi again!

                 

                I have now played around a bit with SolidWorks and the makeTransparent.exe. I made the assumption that the images you sent me is based on that you have shaded views on the drawings. If not... then I do not see a convinent way of doing this. Since non-shaded area will get the background color. So the current solutions works as following...

                 

                1) The macro "makeTransparent.swp" adds a sketch picture with RGB[255,0,255] and sets its transparency to 0,1. This for some reasons removed a bug that made some of the geometry clipped off into the sketch picture.

                2) Macro then saves the drawing sheet as a TIF

                3) The makeTransparent.exe opens the TIF file and makes the background (which has RGB[255,0,255]) to be transparent. If there is any color scale of it, it will keep the scale but change it to gray scale and set transparency. E.g if you have a gradient of RGB[255,0,255] to RGB[0,0,0] it will tone it. This should cover the scenario with anti-aliasing.

                4) And finaly the TIF file is deleted with the macro.

                 

                There is a few settings to play around with in the macro, I made comments to it. Please let me know if it is not clear. The main settings is given here. But please look into the macro and look at the end of it. There could be some tuning needed for the deletion of the TIF file. Or if you like, just remove the deletion of tif files if you like to keep them.

                 

                strBg = "C:\makeTransparent\255-0-255.bmp" 'Give path to background, later to be set at transparent with makeTransparent.exe
                strTargetDir = "C:\makeTransparent\" 'Folder where to store the exported file(s). DO NOT forget "\" at the end of path eg. "C:\export\".

                strExe = "C:\makeTransparent\makeTransparent.exe" 'Path to where the makeTransparent.exe is located

                lTimeOut = 10000 'Timeout after 10 sec (10000) Might need some adjustment depending on performance on computer

                 

                Attached is a zip file with the new exe and the macro. The background with RGB[255,0,255] is also included.

                I would also like to mention that my very simple exe could be replaced with ImageMagick if you want to get more features and further manipulations of the images.

                 

                I also attached the source code of makeTransparent.exe. It was made in Microsoft Visual Basic 2010 Express.

                 

                Please let me know if this works out for you.

                // Daniel

                 

                The macro is based on your first post but with some added code. Bits and pieces for the VBA and for the VB.net is extracted from stackoverflow.com. Credits to that site as a good place to look for help and answers.