VBA to add text to a specific drawing View.

Library for macros
Jacomuller
Posts: 47
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 33
x 4

VBA to add text to a specific drawing View.

Unread post by Jacomuller »

I have a create a macro to add 15 empty views to a drawing. Each view represent a part from a BOM for an assembly. This is for our production team on the shop floor. I have created an array text strings, build-up from a number of Custom properties of each parts. I now want to add a text string to each of the 15 views.

I can add the text strings as text to different positions in the drawing, but I want the text string to be inside a view. How do I do that?

There is another tricky part coming where I want to copy the contents of one view of the part-drawing to one of these empty views.

The end goal is to automatically generate a drawing similar to this:
image.png
by Jacomuller » Tue Jul 30, 2024 12:08 am
Hi @JSculley . thanks again for your input. I got the "clean-up" working so all unused views will be deleted at the end of the macro. I couldn't get the resizing of the views/text to work but that is ok. The next step is to copy a view from one drawing to a specific view in this drawing. I will start a new post with that.
Go to full post
User avatar
JSculley
Posts: 646
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 878

Re: VBA to add text to a specific drawing View.

Unread post by JSculley »

Just activate the view before you call CreateText2 to add the note. Here's an example:

Code: Select all

Dim swApp As SldWorks.SldWorks
Const xStart = 5 * 25.4 / 1000#
Const yStart = 5 * 25.4 / 1000#
Const xGap = 5 * 25.4 / 1000#
Const yGap = 5 * 25.4 / 1000#
Const rowCount = 3
Const colCount = 5
Sub main()
    Dim mDoc As ModelDoc2
    Dim dDoc As DrawingDoc
    Set swApp = Application.SldWorks
    Set mDoc = swApp.ActiveDoc
    Set dDoc = mDoc
    Dim xPos As Double
    Dim yPos As Double
    Dim xCenter As Double
    Dim yCenter As Double
    Dim nextView As View
    Dim viewNote As Note
    For i = 0 To rowCount - 1
        For j = 0 To colCount - 1
            xPos = xStart + xGap * j
            yPos = yStart + yGap * i
            xCenter = xPos + (0.001 - xPos) / 2#
            yCenter = yPos + (0.001 - yPos) / 2#
            Set nextView = dDoc.CreateViewport3(xPos, yPos, 0, 1#)
            dDoc.ActivateView nextView.Name
            Set viewNote = dDoc.CreateText2("View (" & i & "," & j & ")", xCenter, yCenter, 0, 0.125 * 25.4 / 1000#, 0)
        Next j
    Next i
End Sub

Jacomuller
Posts: 47
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 33
x 4

Re: VBA to add text to a specific drawing View.

Unread post by Jacomuller »

Good Morning @JSculley . I fiddled a bit with the code and it is working fine, thanks again for your help UU . It still continuously working on making it better. Is there a way I can set the "wordwrap width" with the code? Keep in mind that I don't have "SOLIDWORKS Document Manager API license key" since I don't have a current Solidworks subscription.

The other thing I need to do at the end of the macro is to "clean-up". A unused views and notes need to be delete.
User avatar
JSculley
Posts: 646
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 878

Re: VBA to add text to a specific drawing View.

Unread post by JSculley »

Jacomuller wrote: Sun Jul 28, 2024 5:38 pm Good Morning @JSculley . I fiddled a bit with the code and it is working fine, thanks again for your help UU . It still continuously working on making it better. Is there a way I can set the "wordwrap width" with the code? Keep in mind that I don't have "SOLIDWORKS Document Manager API license key" since I don't have a current Solidworks subscription.
You can change the line width by accessing the Annotation for the newly created Note and then using the TextFormat object of the Annotation. TextFormat has a LineLength property that controls the note width.

It's also a good practice to use the UserUnit object to convert between document and system units. My original example code assumed inches were being used in the document. If you use the UserUnit object, the actual document units don't matter, so the code is more universal:

Code: Select all

Set viewNote = dDoc.CreateText2("View (" & i & "," & j & ")", xCenter, yCenter, 0, 0.125 * 25.4 / 1000#, 0)
Const noteWidth = 0.5 'Desired note width in document units
Dim ann As Annotation
Dim tf As TextFormat
Dim units As UserUnit
Set units = mDoc.GetUserUnit(swUserUnitsType_e.swLengthUnit)
Dim convFactor As Double
convFactor = units.GetConversionFactor
Set ann = viewNote.GetAnnotation()
Set tf = ann.GetTextFormat(0)
tf.LineLength = noteWidth / convFactor
ann.SetTextFormat 0, False, tf
image.png
The other thing I need to do at the end of the macro is to "clean-up". A unused views and notes need to be delete.
To delete the empty views, loop through them all selecting any that don't reference a model and then delete the selections:

Code: Select all

'Select empty views
Dim retval As Boolean
Dim mExt As ModelDocExtension
Set mExt = mDoc.Extension
Dim vCount As Integer
vCount = dDoc.GetViewCount
Dim v As View
Set v = dDoc.GetFirstView 'This is the sheet format
For i = 1 To vCount - 1
    Set v = v.GetNextView
    Dim refDoc As Object
    Set refDoc = v.ReferencedDocument
    If refDoc Is Nothing Then
        retval = mExt.SelectByID2(v.Name, "DRAWINGVIEW", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
    End If
Next i
'Delete selected empty views
retval = mExt.DeleteSelection2(swDelete_Absorbed)
Jacomuller
Posts: 47
Joined: Wed Mar 01, 2023 6:55 pm
Answers: 2
x 33
x 4

Re: VBA to add text to a specific drawing View.

Unread post by Jacomuller »

Hi @JSculley . thanks again for your input. I got the "clean-up" working so all unused views will be deleted at the end of the macro. I couldn't get the resizing of the views/text to work but that is ok. The next step is to copy a view from one drawing to a specific view in this drawing. I will start a new post with that.
Post Reply