Moving dimensions via macro

I apologize for the length of this post. I’ve divided it into sections so you can skip straight to the question, then come back to the scenario if you need to. I’ve posted a related question in another thread—https://forum.solidworks.com/thread/236167—but this is definitely a different issue. I’m working at a company with sensitive information, so I won’t be able to show images, so I’ll try to be descriptive of what the scenario is and what’s going on.

Basic Question

How can I reference/select dimensions via API/macro in a specific sketch in a specific drawing view to be able to edit its arrangement (centered, etc.) or move it? I believe I’ve narrowed it down to the fact that it’s not the arranging/moving that’s the issue, but actually selecting the dimension that I’m trying to arrange. Here’s the backstory…

Model and Drawing Setup

The big picture goal is automating the drawing process of some standard assemblies that we have. For the assembly, imagine a box whose cover opens from the end. The end with this cover is considered the front of the box. There is also an assembly mounted inside the box. Typically, for a drawing, we pull in a right side view of the assembly as “Drawing View1,” then a projected front view is pulled off as “Drawing View2” and a projected rear view is pulled off as “Drawing View2.” “Drawing View1” has four standard dimensions of features that are shown on every drawing. “Drawing View2” has seven standard dimensions of features that are shown on every drawing. In the assembly, I have a display states set up: ENTIRE CASE has all used components shown, NO LID has the cover and all its hardware hidden so that the contents can be seen. On the drawing, in “Drawing View2” I can just select the NO LID display state to show that end of the box and be able to see the contents of the assembly inside. Some of the standard dimensions in that view are of the internal assembly. I realize that images would definitely help with visualizing this, but are you tracking so far?

Current State of Macro

That’s not the entire drawing, but that sets up the scenario for my question. That’s a description of how we do it manually, and the end drawing format can’t change. So far, I have all my views and dimensions being imported automatically via a macro. Unfortunately, I haven’t found a way to select a display state in a drawing view via Solidworks API, so I had to perform a workaround. The macro imports the views in these steps:

  • Import right side view as “Drawing View1” in the ENTIRE CASE display state at a specific location
  • Switch back to the assembly
  • Change the assembly to the NO LID display state
  • Switch back to the drawing
  • Import front view as “Drawing View2” at a specific location
  • Align “Drawing View2” horizontally by origin to “Drawing View1”
  • Switch back to the assembly
  • Change the assembly to the ENTIRE CASE display state
  • Switch back to the drawing and imports the remaining drawing views

The next step in the macro is to import annotations which include all the unsuppressed dimensions from the assembly that are marked to be imported. This includes the four dimensions in “Drawing View1” and the seven dimensions in “Drawing View2.” This actually brings me to the point in my process where my other question came up (https://forum.solidworks.com/thread/236167).

Question at Hand

And it finally brings me to the point of this post. I’m still working out the auto-arranging of my dimensions. Using the auto-dimension command in Solidworks doesn’t work, so I’m “manually” through the macro setting each dimension. Some are being moved to very specific location while others are just being centered along the dimension line. This is working great in “Drawing View1,” but not so much in “Drawing View2.” Through recording mini-macros and digging through the Solidworks API help, this is the code that I have for adjusting the first dimension location in “Drawing View1”:

swDimension = swModel.ActivateView("Drawing View1")<br>swDimension = swModel.Extension.SelectByID2("D1@03U 3-3 SKETCH@730301413000-1@DV1", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)<br>swDimension = swModel.EditDimensionProperties2(0, 0, 0, "", "", True, 9, 2, True, 12, 12, "", "", True, "", "", True)<span class="line-numbers-rows"><span>‍</span><span>‍</span><span>‍</span></span>

For the sake of not making this post any more lengthy, we’ll assume that all the variables are defined and setup correctly. You’ll notice that I gave the sketches custom names so I can refer to them easily. The above lines of code work fine for all the dimensions in that view. What has not worked is using this same code for the dimensions in “Drawing View2.” Here are some things I’ve tried:

  • I have altered the above code by changing the view references to “Drawing View2” and the sketch reference to the custom-named sketch in that view.
  • I have Recorded a mini-macro editing the dimension in that view. I can record a macro editing the dimensions in View 1, reset the dimensions, and run the macro—it runs fine and changes the dims. If I record a macro editing the dimensions in View 2, reset the dimensions, and run the macro, nothing happens. Interestingly, when I record that second macro, I get something like this: boolstatus = Part.Extension.SelectByID2("D1@7300 AA STANDARD FRAME SKETCH@730301413000-1@DV1", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0). Even though I have verified that it is in fact “Drawing View2,” it still records as “Drawing View1.” Changing it to View2 in the recorded macro does nothing.
  • If I record a single macro of editing dimensions in View1 and View2, I can run the macro and it will change the dimensions in View1, but not the ones in View2.
  • I thought maybe it was getting stuck on View1 and not being able to move to View2, even though I’m using the command swModel.ActivateView("Drawing View2") to activate that view, so I tried deleting all the views except View2. I then tried recording a macro of editing a dimension in that view, but I couldn’t even edit the dimension. If I try to edit a dimension in that view, I get this warning: “This dimension cannot be edited. It is a deleted dimension shown as dangling.” I’m not seeing that warning as an issue necessarily, but I think it gives a clue as to what’s going on with not being able to select a dimension in that view via macro.

 

Back to the Basic Question

How can I reference/select dimensions in a specific sketch in a specific drawing view to be able to edit its arrangement or move it?

 

Thank you for being amazing and making it to the bottom of this post. Again, I apologize for its length, but I couldn’t really ask the question without laying down the scenario and stating what I had tried so far. Thank you in advance for any input you might have.

SolidworksApi/macros
Answers 5
SF
SF 2021-04-08 Best Answer

I think I may have answered my own question, at least to an extent. I'll post it here in case someone else happens to have the same issue.

As I was writing my diatribe, I mentally noted a couple things that seemed linked: when I recorded a macro on "Drawing View2" it captured it as "Drawing View1", and when I deleted all views except View 2 the dimension in that view became disassociated with any view. These two things combined made me realize that even though some dimensions were shown on different views, they were all associated with View 1. For some reason, this meant I couldn't go back to edit them through a macro, even though recording a macro would capture the edits.

Originally, I was adding all views first, then importing all annotations. I changed it so that I'm adding a view and importing its annotations, then adding the next view and importing its annotations, and so on. This might not be the best fix, but it seems to have fixed the issue at least as a workaround.

SF
SF 2021-04-08

Hey Justin,

I've been messing around with this to try to figure out my issue over here:  . So far, I haven't had any luck. As I mention in there, I have the first offset at .40" and the subsequent at .25". For some reason, my imported dimensions aren't snapping to those offsets. I'm trying to get the macro to snap the dimensions to the offset distances that are set in Options. Whether it's as I drop in each dimension or after they're all in the drawing doesn't matter to me. Any idea how I can snap the dimensions to the offset dimensions that are set in Options? Auto-dimension doesn't do it.

Thanks.

RE
RE 2021-04-08

Hi Bryson

Unfortunately the macro recorder doesn't always give the best code.

As a caveat I haven't done much with the Drawing side of the API so there's probably a better way.

Anyway - here's a really annoying macro that cycles through all of the display dimensions in all of the views in all of the sheets.  It selects them and pops up a message box with some information.

This is just for illustration purposes.

In my simple example I use a case statement and when it finds a particular dimension it pops up

Here's the code.. hopefully it will help

Option Explicit

Dim swApp As SldWorks.SldWorks

Sub main()

   Set swApp = Application.SldWorks

   Dim swModel As SldWorks.ModelDoc2
   Set swModel = swApp.ActiveDoc

   If swModel Is Nothing Then Exit Sub

   If swModel.GetType = swDocDRAWING Then DoShiz swModel

End Sub

Private Sub DoShiz(swDraw As SldWorks.DrawingDoc)
   Dim vViews As Variant
   vViews = swDraw.GetViews 'Array of Arrays
   Dim swView As SldWorks.View
   Dim SheetNum As Long
   Dim ViewNum As Long
   Dim DimNum As Long
   Dim vDisplayDimensions As Variant
   Dim swDisplayDimension As SldWorks.DisplayDimension
   Dim swDimension As SldWorks.Dimension
   Dim swAnnotation As SldWorks.Annotation
   Dim swFeature As SldWorks.Feature
   Dim msg As String
   For SheetNum = 0 To UBound(vViews)
      For ViewNum = 0 To UBound(vViews(SheetNum))
         Set swView = vViews(SheetNum)(ViewNum)
         MsgBox swView.GetName2 & vbCr & swView.GetDisplayDimensionCount & "Dims"
         If swView.GetDisplayDimensionCount > 0 Then
            vDisplayDimensions = swView.GetDisplayDimensions
            For DimNum = 0 To UBound(vDisplayDimensions)
               msg = swView.GetName2 & vbCr
               Set swDisplayDimension = vDisplayDimensions(DimNum)
               msg = msg & swDisplayDimension.GetNameForSelection & vbCr
               Set swDimension = swDisplayDimension.GetDimension
               If Not swDimension Is Nothing Then
                  msg = msg & swDimension.Name & vbCr
                  Set swFeature = swDimension.GetFeatureOwner
                  msg = msg & swFeature.Name & vbCr

               End If
               Set swAnnotation = swDisplayDimension.GetAnnotation
               swAnnotation.Select3 False, Nothing
               MsgBox msg

               Select Case swDisplayDimension.GetNameForSelection
                  Case "D1@Sketch1@Part1-1@Drawing View1"
                     MsgBox "BINGO!"
                  Case Else
               End Select
            Next DimNum
         End If
      Next ViewNum
   Next SheetNum

End Sub

comment imagecomment image
JP
JP 2021-04-08

I dont know if it will help, but i asked for a macro to auto arrange dims. maybe there is something in there that may help you.

 

JM
JM 2023-03-09

I am trying to solve a similar problem. Did you figure out a way to position the dimension via the macro. I can't seem to find the command. Looking at your post above I do not see anything in the commands that specifys position. Can you help?