Move dimensions in drawing

I am looking to make a macro that will move the position of a dimension to a particular x,y coordinate on the drawing.  Any suggensions?

SolidworksApi macros
Answers 8
SF
SF 2011-10-26 Best Answer

I would just run the macro recorder and select a dimension move it a bit. stop the recorder move it back. then play the the macro and make sure it works ok. If it does then go into VBA or whatever program and tweak the dimensions a little bit and find out where you want things to land. I don't necesarily know if there is a perfect answer.

One thing to remember is that all units in Solidworks API whether C# VBA, etc is that all units are in meters, and can be a huge pain for programs like this.

Russ Saari

SF
SF 2011-10-26

This does not work. 

It uses:

boolstatus = Part.Extension.SelectByID2("RD5@Drawing View8", "DIMENSION", 0.1915416680361, 0.2626097422845, 0, False, 0, Nothing, 0)

Which does not move the dimension for some reason.  I think I need to use:

boolstatus = Annotation.SetPosition(0.1915416680361, 0.2626097422845, 0)

but I don't know how to set this up. 

JG
JG 2011-10-28

Mike,

You are correct, you need to use the SetPosition method to move the dimensions.  Use the recorder to get the dimension you want to move and then use the SetPosition method to move it.  You can find examples of this in the API Help and I have also added an example of the code I typically use to move dimensions. (code in vb.net)

boolstatus = swDoc.Extension.SelectByID2("D1@Sketch3@PROCO Series 271.SLDDRW", "DIMENSION", 0.2902165986395, 0.2188633333333, 0, False, 0, Nothing, 0)

       

move_dimension(500, 500)

    Function move_dimension(ByVal x As Integer, ByVal y As Integer) As Boolean

        swApp = CreateObject("SldWorks.Application")

        'swDoc = swApp.ActiveDoc

        'swSelMgr = swDoc.SelectionManager

        'swNote1 = swSelMgr1.GetSelectedObject3(1)

        'swAnn1 = swNote1.GetAnnotation

        Dim MyDim As Object

        Dim MyDimension As Object

        MyDim = swDoc.ISelectionManager.GetSelectedObject5(1)

        MyDimension = MyDim.GetAnnotation()

        boolstatus = MyDimension.setposition(x / 1000 , y / 1000 , 0)

        swDoc.GraphicsRedraw2()

    End Function

SF
SF 2011-10-28

Hi,

Is there a macro to check the overlap of dimensions with views, balloons & other dimensions and if so reposition them accordingly in a drawing?

Thanks,

Subbu

SF
SF 2011-10-28

Here's a solution that worked for me from the awesome guys at API Support:  Thanks Joe!

Sub main()

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swAnn As SldWorks.Annotation
Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long
Dim vAnnPos As Variant

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager

swModel.ClearSelection2 True
boolstatus = swModel.ActivateView("Drawing View1")
boolstatus = swModel.Extension.SelectByID2("", "EDGE", 0.296970669250797, 0.706578975418062, 249.964418001907, False, 0, Nothing, 0)

Dim myDisplayDim As SldWorks.DisplayDimension
Set myDisplayDim = swModel.AddHorizontalDimension2(0.304260970541933, 0.794062590909949, 0)
swModel.ClearSelection2 True
boolstatus = swModel.ActivateSheet("Sheet1")
swModel.ClearSelection2 True
boolstatus = swModel.ActivateView("Drawing View1")

boolstatus = swModel.Extension.SelectByID2("RD1@Drawing View6", "DIMENSION", 0.304260970541933, 0.795277641125139, 0, False, 0, Nothing, 0)
Set myDisplayDim = swSelMgr.GetSelectedObject6(1, -1)

Set swAnn = myDisplayDim.GetAnnotation()
vAnnPos = swAnn.GetPosition

Debug.Print "      AnnPos       = (" & vAnnPos(0) * 1000# & ", " & vAnnPos(1) * 1000# & ", " & vAnnPos(2) * 1000# & ") mm"

boolstatus = swAnn.SetPosition(0.1, 0.1, 0.1)


End Sub

SF
SF 2011-11-02

Hi Mike,

I tried running this macro in one of our drawings and it doesnt yield the result. I hope the macro has values hardcoded and it works only for the specific drawing and not for all the drawings. Is it so?

If not can you share the generic macro if you have one?

Thanks,

SF
SF 2011-11-02

This code requires that the drawing be open and active, that the drawing be on sheet 1, that you have a drawing view 6 on the sheet, and that there be a dimension labeled "RD1@Drawing View 6"  It should work with any drwing tht meets these requirements. 

SF
SF 2011-11-21

Hi Kishore,

The attached macro can select all the dimension in the drawing sheet and can AUTO-Arrange them.Once auto-arrrange there is no question of overlapping.

Thanks

Anjani ojha