1 Reply Latest reply on Sep 21, 2014 8:09 AM by Yong Ning

    How to use the VBA connect edrawing

    Yong Ning

      I will only VBA programming, but not with C programming

       

      *************************************

      Solidworks help file →

      Use of the SolidWorks API assumes that you are familiar with VBA, VB.NET, Visual C#, Visual C++ 6.0, or Visual C++/CLI.

      Help me, How to use the VBA connection, edrawing

      ****************************

      for example

       

      3.jpg

      Quote Solidworks 2012 type library

      VBA connect Solidworks Code.

        Dim SwApp As SldWorks.SldWorks,

          Set SwApp = GetObject(, "SldWorks.Application")

       

      2.jpg

      Help me

      Quote EModelView 2012 Type Library

         Help me, How to use the VBA connection, edrawing

       

       

      *******************************

      Follow code , don't open SaddleS.Easm file.

       

      Private Sub llll()

        Dim e As New EModelViewControl, FileName

        FileName = "E:\MyWorkSummary\BESmodel\卧式储罐\Tank2008\SaddleS.Easm"

        e.OpenDoc FileName, False, False, False, ""

      End Sub

       

       

      **********************************

      API: eDrawings 2012 API Example (VB.NET, C#) | SOLIDWORKS Forum

      https://forum.solidworks.com/docs/DOC-2232?sr=search&searchId=581aeb12-b1ac-44bf-b375-66a8325dbfcc&searchIndex=2

       

      ******************************

        • Re: How to use the VBA connect edrawing
          Yong Ning

          Help me , conver follow code to VBA

           

          Dim EMVControls As New EModelView.EModelViewControl

           

          EMVControls = New EModelView.EModelViewControl()

          EMVControls.OpenDoc( "C:\Visual Basic\Design Table Practice for VB\test.eprt", True, True, True,"")

          EMVControls = Nothing

           

           

           

          how to open a file from Visual Basic Express 2008 | SOLIDWORKS Forum

          https://forum.solidworks.com/thread/32863?start=75&tstart=0

           

          **************************************************

           

          Help with opening and saving edrawings in VB2008 | SOLIDWORKS Forum

          https://forum.solidworks.com/message/44844?sr=search&searchId=b03ffe09-dea9-448b-8897-22b4691162fe&searchIndex=19#44844

           

           

          ***************************************************

          EModelMarkupControl - 必应

          Reset BrowseForFile on vb.net Form...

          http://www.experts-exchange.com/Programming/Languages/.NET/Q_24092768.html

           

           

          Imports EModelView

          Imports AxEModelView

          Imports EModelViewMarkup

          Imports EdmLib

          Imports System.Drawing.Printing

           

          Public Class Form1

          Dim objEDrw As Object

              Dim MyStopWatch As New System.Diagnostics.Stopwatch()

          Dim IsFileLoaded As Boolean = False

          Dim IsFilePrinted As Boolean = False

           

           

          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

           

          '********************************************

          'Add available printers to the dialog box

          '--------------------------------------------

          ' Find all printers installed and add then to the drop down list

          For Each pkInstalledPrinters In PrinterSettings.InstalledPrinters

          cboInstalledPrinters.Items.Add(pkInstalledPrinters)

          Next pkInstalledPrinters

           

          ' Set the combo to a dummy printer to make sure that they do choose a printer

          cboInstalledPrinters.SelectedText = "--"

           

           

          End Sub

           

          'Step One - Browse for the assembly:

           

          Private Sub btn_BrowseAssembly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_BrowseAssembly.Click

          'Log into the vault

          '---------------------------------

          vault.LoginAuto("Astec", Me.Handle.ToInt32)

           

          'Open File Dialog, Set the directory to the root of the vault,

          'and limit selection to assemblies only

          '----------------------------------------------------------------

          OpenFileDialog1.Title = "Please Select A File"

          OpenFileDialog1.InitialDirectory = vault.RootFolderPath

          OpenFileDialog1.Filter = "SolidWorks drawings|*.slddrw"

           

          'If they selected a file successfully, then put the file name in the text box,

          'get the versions of the file and put them in the version drop down with

          'a 0 as the default selected item

          '--------------------------------------------------------------------------------

          If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then

           

          filename = OpenFileDialog1.FileName

          txtbox_DisplayAssemblyName.Text = OpenFileDialog1.FileName

          file = vault.GetFileFromPath(filename)

                      folder = vault.GetFolderFromPath(filename.Substring(0, filename.Length - 17))

           

          'This method put the version into the drop-down going lowest number to highest... 0, 1, 2, 3, etc.

          'For i As Integer = 0 To file.CurrentVersion - 1

          ' ComboBox_VersionNum.Items.Add(i + 1)

          'Next

           

          'This method seems like it would better, it puts the version in the drop-down going highest to lowest... 30, 29, 28, 27, etc.

          Dim i As Integer = file.CurrentVersion

          While i > 0

          ComboBox_VersionNum.Items.Add(i)

                          i = i - 1

          End While

           

          ComboBox_VersionNum.SelectedText = 0

           

          AxEModelViewControl1.Refresh()

           

          'Let's make sure they have an assembly name in the text box before enabling the next button

          If txtbox_DisplayAssemblyName.Text <> "" Then

                          ComboBox_VersionNum.Enabled = True

          End If

          End If

          End Sub

           

          'Step 2 Choose a Version Number

          Private Sub ComboBox_VersionNum_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox_VersionNum.SelectedIndexChanged

           

          txtbox_JobNumber.Enabled = True

                  AxEModelViewControl1.CloseActiveDoc("")

          '*****************************************************************************************************************************

          'Begin to load the file into the eDrawings Control on the form -

                  'This is necessary to show the drawing on the form and create the control that you need to work with

          '-----------------------------------------------------------------------------------------------------------------------------

           

          'Get a copy of the version chosen brought to the local cache

          file.GetFileCopy(Me.Handle.ToInt32, ComboBox_VersionNum.SelectedItem, folder.ID, EdmGetFlag.EdmGet_MakeReadOnly, "")

           

          'Make sure that the file exists before loading

          If System.IO.File.Exists(filename) Then

          'this form.the edrawings control on the form.open the file(path to the file, IsTemp, PromptToSave, Is ReadOnly, CommandString always empty

          Me.AxEModelViewControl1.OpenDoc(filename, False, False, True, "")

          MyStopWatch.Reset()

          MyStopWatch.Start()

          Else

          MsgBox("This file wasn't copied to the local cache successfully:" & vbCrLf & filename)

          End If

          '-----------------------------------------------------------------------------------------------------------------------------

          'End code to load the file into the eDrawings Control on the form

          '*****************************************************************************************************************************

           

           

           

          End Sub

           

          'Step 3 Enter the Job Number

          Private Sub txtbox_JobNumber_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbox_JobNumber.TextChanged

          cboInstalledPrinters.Enabled = True

           

          End Sub

           

          'Step 4 Choose a Printer

          Private Sub cboInstalledPrinters_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboInstalledPrinters.Click

          1002:

          If IsFileLoaded Then

          btn_Go.Enabled = True

          Else

          GoTo 1002

          End If

           

          End Sub

           

          'Step 5 Click Go Button

          Private Sub btn_Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Go.Click

          1001:

          If IsFileLoaded Then

          '*****************************************************************************************************************************

          'Begin code to stamp the drawing with job number from the input on the form and today's date

          'In order to stamp the job and date, we have to create an image that eDrawings can use as a stamp.

          'I'm starting with a white rectangle as the base image to simplify the code and saving it out as a tiff for quality.

          '-----------------------------------------------------------------------------------------------------------------------------

          'Contruct a bitmap object froma jpg's filename:

          Dim OriginalStampPath As String = vault.RootFolderPath + "\_Administrator Files\BatchPlotStamp\StarterStamp.jpg"

          Dim StampOriginal As Bitmap = New Bitmap(OriginalStampPath)

           

          'Obtain a Graphics object from and for that bitmap:

          Dim StampCanvas As Graphics = Graphics.FromImage(StampOriginal)

           

          'Get today's date

          Dim TodayFullDate As Date = Date.Today

           

          'Get today's month

          Dim TodayMonth As String = TodayFullDate.Month

           

          'Lets turn the string for the month into an integer... ex. Decemeber = 12

          If TodayMonth = "January" Then

                          TodayMonth = 1

          ElseIf TodayMonth = "February" Then

                          TodayMonth = 2

          ElseIf TodayMonth = "March" Then

                          TodayMonth = 3

          ElseIf TodayMonth = "April" Then

                          TodayMonth = 4

          ElseIf TodayMonth = "May" Then

                          TodayMonth = 5

          ElseIf TodayMonth = "June" Then

                          TodayMonth = 6

          ElseIf TodayMonth = "July" Then

                          TodayMonth = 7

          ElseIf TodayMonth = "August" Then

                          TodayMonth = 8

          ElseIf TodayMonth = "September" Then

                          TodayMonth = 9

          ElseIf TodayMonth = "October" Then

                          TodayMonth = 10

          ElseIf TodayMonth = "November" Then

                          TodayMonth = 11

          ElseIf TodayMonth = "December" Then

                          TodayMonth = 12

          End If

           

                      'Get today's day of the month

          Dim TodayDay As String = TodayFullDate.Day

           

          'Get today's year

          Dim TodayYear As String = TodayFullDate.Year

           

          'Create the string that will be written to the image - the job number from the text box on the form + date in the format 00/00/0000

          Dim strStamp As String = txtbox_JobNumber.Text + " " + TodayMonth + "/" + TodayDay + "/" + TodayYear

           

          'Draw the stamp string ono the jpg:

          StampCanvas.DrawString(strStamp, New Font("Arial", 22, FontStyle.Bold), New SolidBrush(Color.Black), 0, 0)

           

          'Save the stamped image out to a file and convert it to a tiff image format for best quality

          NewStampPath = vault.RootFolderPath + "\_Administrator Files\BatchPlotStamp\" & file.Name & "-" & TodayMonth & TodayDay & TodayYear & "-" & txtbox_JobNumber.Text & ".tif"

          StampOriginal.Save(NewStampPath, System.Drawing.Imaging.ImageFormat.Tiff)

           

          'Create reference to the eDrawings Markup control

          Dim MarkupControl As New EModelViewMarkup.EModelMarkupControl

          MarkupControl = AxEModelViewControl1.CoCreateInstance("EModelViewMarkup.EModelMarkupControl")

           

          'Stamp the drawing - Path to stamp image, x coordinate in drawing units, y coordinate in drawing units, image width, image height

          MarkupControl.AddStamp(NewStampPath, 0.766, 0.122, 0.165, 0.019)

           

          '-----------------------------------------------------------------------------------------------------------------------------

          'End code to stamp the drawing with job number from the input on the form and today's date

          '*****************************************************************************************************************************

           

           

          System.Threading.Thread.Sleep(5000)

          '*****************************************************************************************************************************

          'Begin code to print the drawing

          '-----------------------------------------------------------------------------------------------------------------------------

          AxEModelViewControl1.SetPageSetupOptions(EMVPrintOrientation.eLandscape, Printing.PaperKind.Standard11x17, 0, 0, 1, 7, cboInstalledPrinters.SelectedItem, 0, 0, 0, 0)

          AxEModelViewControl1.Print3(False, "", False, False, False, EMVPrintType.eScaleToFit, 0, 0, 0)

           

          Else

          GoTo 1001

          End If

           

          '-----------------------------------------------------------------------------------------------------------------------------

          'End code to print the drawing

          '*****************************************************************************************************************************

           

          End Sub

           

          Sub StartOver(ByVal sender As System.Object, ByVal e As System.EventArgs)

           

          End Sub

           

          Private Sub btn_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Close.Click

          'Close the form

          '------------------

          Me.Close()

          End Sub

           

          'If the drawings fails to load:

          Private Sub AxEModelViewControl1_OnFailedLoadingDocument(ByVal sender As Object, ByVal e As AxEModelView._IEModelViewControlEvents_OnFailedLoadingDocumentEvent) Handles AxEModelViewControl1.OnFailedLoadingDocument

          IsFileLoaded = False

          MyStopWatch.Stop()

          MessageBox.Show("Failed loading" & vbCrLf & "Time (ms): " & MyStopWatch.ElapsedMilliseconds)

           

          End Sub

           

          'If the drawing fails to print

              Private Sub AxEModelViewControl1_OnFailedPrintingDocument(ByVal sender As Object, ByVal e As AxEModelView._IEModelViewControlEvents_OnFailedPrintingDocumentEvent) Handles AxEModelViewControl1.OnFailedPrintingDocument

          MyStopWatch.Stop()

          MessageBox.Show("Failed Printing" & vbCrLf & "Time (ms): " & MyStopWatch.ElapsedMilliseconds)

          End Sub

           

          'If the Drawing finishes loading

          Private Sub AxEModelViewControl1_OnFinishedLoadingDocument(ByVal sender As Object, ByVal e As AxEModelView._IEModelViewControlEvents_OnFinishedLoadingDocumentEvent) Handles AxEModelViewControl1.OnFinishedLoadingDocument

          IsFileLoaded = True

          'Leaving this commented bit in here so I can use it to test for loading as needed:

          'MyStopWatch.Stop()

          'MessageBox.Show("Finished loading" & vbCrLf & "Time (ms): " & MyStopWatch.ElapsedMilliseconds)

          End Sub

           

          'If the Drawing finishes Printing

          Private Sub AxEModelViewControl1_OnFinishedPrintingDocument(ByVal sender As Object, ByVal e As AxEModelView._IEModelViewControlEvents_OnFinishedPrintingDocumentEvent) Handles AxEModelViewControl1.OnFinishedPrintingDocument

          IsFilePrinted = True

          End Sub

           

          End Class

           

            '************************************ Markups Tab ***************************************

              'Note: The GUID that is used for the EModelViewMarkupControl is the non-version-specific GUID.

              Private Sub getComm_Click(sender As Object, e As EventArgs)

            Dim markup As EModelMarkupControl = DirectCast(axEModelViewControl1.CoCreateInstance("{5BBBC05A-BD4D-4e3b-AD5B-51A79DFC522F}"), EModelMarkupControl)

                  Dim count As Integer = markup.CommentCount

                  Dim n As Integer = 0

                  Dim comInput As String

                  commSel.Items.Clear()

                  For n = 0 To count - 1

                      comInput = markup.get_CommentName(n)

                      commSel.Items.Add(comInput)

                  Next

                  '0 is the default selection

                  commSel.SelectedIndex = 0

              End Sub

              Private Sub setComm_Click(sender As Object, e As EventArgs)

            Dim markup As EModelMarkupControl = DirectCast(axEModelViewControl1.CoCreateInstance("{5BBBC05A-BD4D-4e3b-AD5B-51A79DFC522F}"), EModelMarkupControl)

                  Dim comID As Integer = markup.get_CommentID(commSel.SelectedIndex)

                  markup.ShowComment(comID)

              End Sub

              ' Open a browser to specify a stamp to use inside of eDrawings

              Private Sub findStamp_Click(sender As Object, e As EventArgs)

                  openFileDialog1.ShowDialog()

                  stampFile.Text = openFileDialog1.FileName

              End Sub

              Private Sub placeStamp_Click(sender As Object, e As EventArgs)

            Dim markup As EModelMarkupControl = DirectCast(axEModelViewControl1.CoCreateInstance("{5BBBC05A-BD4D-4e3b-AD5B-51A79DFC522F}"), EModelMarkupControl)

                  Dim xVal As Single = CSng(Convert.ToDouble(xStamp.Text))

                  Dim yVal As Single = CSng(Convert.ToDouble(yStamp.Text))

                  Dim wVal As Single = CSng(Convert.ToDouble(wStamp.Text))

                  Dim hVal As Single = CSng(Convert.ToDouble(hStamp.Text))

                  markup.AddStamp(stampFile.Text, xVal, yVal, wVal, hVal)

              End Sub

              Private Sub saveMarkup_Click(sender As Object, e As EventArgs)

            Dim markup As EModelMarkupControl = DirectCast(axEModelViewControl1.CoCreateInstance("{5BBBC05A-BD4D-4e3b-AD5B-51A79DFC522F}"), EModelMarkupControl)

                  markup.ShowSaveMarkup("", True)