Inventor General Discussion

Reply
Valued Contributor
ccoomes
Posts: 51
Registered: ‎01-21-2008
Message 1 of 7 (228 Views)
Accepted Solution

Re-Select Selected Items after Changing Dimension - VBA

228 Views, 6 Replies
02-25-2014 01:41 AM

I have some code that changes the text and precision of selected dimensions as follows:

 

'Set a reference to the select set of the active document.
Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oDrawingDims() As DrawingDimension

Dim oItemsCount As String
oItemsCount = oDoc.SelectSet.Count

Dim oDimText As String, oDimPrecision As Long

oDimText = gDimText
oDimPrecision = gDimPrecision

If oItemsCount = 0 Then
Call MessageBoxes.NoDimensionSelected
Exit Sub
Else
Dim a As Long
a = 0
Dim i As Long
For i = 1 To oDoc.SelectSet.Count
If Not oDoc.SelectSet.Item(i) Is Nothing Then
If TypeOf oDoc.SelectSet.Item(i) Is DrawingDimension Then
a = a + 1
ReDim Preserve oDrawingDims(0 To a)
Set oDrawingDims(a) = oDoc.SelectSet.Item(i)
Else
Call MessageBoxes.NoDimensionSelected
Exit Sub
End If
End If
Next
Dim b As Long
For b = 1 To a
oDrawingDims(b).Text.FormattedText = oDimText
oDrawingDims(b).Precision = oDimPrecision
Next
End If

End Sub

 

The problem I have is that after it is modified the Dimension correctly, it de-selects all the items so there is nothing selected.

 

How can I add some more code in VBA so it will keep the selected items selected?

 

Please note that I did not create the code above, I copied if from the web and changed it to suit.  I can't remember who created the original code, but i has saved me a lot of time and effort.

 

If anyone can guide me on how to re-select the selected items that will save us even more time.

ADN Support Specialist
philippe.leefsma
Posts: 630
Registered: ‎06-02-2009
Message 2 of 7 (198 Views)

Re: Re-Select Selected Items after Changing Dimension - VBA

03-03-2014 12:58 AM in reply to: ccoomes

Any transacted operation will clear the select set, for example modifying a dimension properties. You need to store the content of the select set before doing any modification. You can for example create and populate an ObjectCollection or a simple array of variants.

 

I hope it helps,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Valued Contributor
ccoomes
Posts: 51
Registered: ‎01-21-2008
Message 3 of 7 (193 Views)

Re: Re-Select Selected Items after Changing Dimension - VBA

03-03-2014 02:20 AM in reply to: philippe.leefsma

Hi Philippe,

 

I did think it would be something like that.

 

I have tried to get create a Object Collection but I cannot work out how to re-select them and add them to it.

 

Could you please advise on the code needed to create the object collection, add the dimensions to it and then re-select the collection?

 

Clive.

ADN Support Specialist
philippe.leefsma
Posts: 630
Registered: ‎06-02-2009
Message 4 of 7 (190 Views)

Re: Re-Select Selected Items after Changing Dimension - VBA

03-03-2014 02:27 AM in reply to: ccoomes

You create the ObjectCollection with TransientObjects.CreateObjectCollection. You should be able to find plenty of examples in the API Help Files.

 

ObjectCollection.Add to add objects to it.

 

You can re-select the objects with SelectSet.Select.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Valued Contributor
ccoomes
Posts: 51
Registered: ‎01-21-2008
Message 5 of 7 (175 Views)

Re: Re-Select Selected Items after Changing Dimension - VBA

03-04-2014 12:13 AM in reply to: philippe.leefsma

Hi Phillipe,

 

I have tried to work out the VBA code by using the Inventor API Help and Examples and I cannot get it to work.

 

Would you be able to supply me the code to get the selected items into a object collection and then how to recall them?

 

There will normally be more than 1 item selected at a time.

 

Many thanks in advance for your assistance.

ADN Support Specialist
philippe.leefsma
Posts: 630
Registered: ‎06-02-2009
Message 6 of 7 (172 Views)

Re: Re-Select Selected Items after Changing Dimension - VBA

03-04-2014 12:28 AM in reply to: ccoomes

How about that:

 

Sub DimTest()

    Dim doc As DrawingDocument
    Set doc = ThisApplication.ActiveDocument

    Dim collec As ObjectCollection
    Set collec = ThisApplication.TransientObjects.CreateObjectCollection
    
    Dim obj As Variant
    For Each obj In doc.SelectSet
        collec.Add obj
    Next

    For Each obj In collec
        If TypeOf obj Is DrawingDimension Then
            Dim drawDim As DrawingDimension
            Set drawDim = obj
            drawDim.HideValue = True
            drawDim.OverrideModelValue = True
            drawDim.Text.FormattedText = "ADN rules!"
        End If
    Next

    For Each obj In collec
       doc.SelectSet.Select obj
    Next
End Sub

 

 

______________________________________________________________

If my post answers your question, please click the "Accept as Solution"

button. This helps everyone find answers more quickly!

 



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Valued Contributor
ccoomes
Posts: 51
Registered: ‎01-21-2008
Message 7 of 7 (167 Views)

Re: Re-Select Selected Items after Changing Dimension - VBA

03-04-2014 12:34 AM in reply to: philippe.leefsma

Hi Phillipe,

 

That is perfect.  I will modify the code to incorporate your method.

 

Many thanks you have saved us a lot of time and effort.

 

Thanks again.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.