Connecting Handles with text

Connecting Handles with text

Kahva
Contributor Contributor
817 Views
6 Replies
Message 1 of 7

Connecting Handles with text

Kahva
Contributor
Contributor

Hello, 

 

I'm currently working on a project where I want to associate a text mark with a drawn polyline. I don't know how to do that. It's maybe better to describe what I want VBA code to do. I want to draw a polyline, mark it with text, and export that to excel. So I assume, when adding new polyline with text, I should find if the new polyline has text attached, and if not - Add a text mark. If there is already text associated with that text mark, it means it is not a new polyline. Pseudocode goes something like:

 

-Draw polyline

-Add a text mark or a number to that polyline (posslibly in center of polyline- Centroid?)

-Export to excell table

 

- Draw new polyline, mark with next text mark.

 

Text marks aren't defined, it can be X1,X2,X3, or simple numbers 1,2,3,... etc

 

For every new drawn object, assign next text mark in ascending order.

 

Currently I have a code that is just adding text for every ployline drawn, and if I add a new polyline, the code just adds two text marks on old polyline and one on new. Code basicaly gets every polyline object (every single time) and adds a text for that object. 

 

0 Likes
818 Views
6 Replies
Replies (6)
Message 2 of 7

Ed__Jobe
Mentor
Mentor

Since you didn't show your code (if you do, make sure to use a code window with the </> button), all I can say is that you can save the LWPOLYLINE.ObjectID to the spreadsheet. Before you add a text item, check the current lwp's ObjectID against the list of oid's stored. It might be easier if you loaded the column of oid's into a collection, then trying to add an oid to the collection would produce an error if it already exists.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 7

Kahva
Contributor
Contributor

Sorry, but my code is very crude, and currently not working. I'm having doubts whether to realise it through excel VBA or ACAD VBA, depending on my possibility to connect to autocad from excel or viceversa. In the moment, half of the project is in Acad VBA and half in excel VBA. Could you clarify why am I not able to create a selection set of Acad objects using filter parameters in Excel VBA, with open Acad drawing? 

 

Thank you for your patience as I'm just starting with VBA.

 

Sub SelSet()

Dim SelSet As AcadSelectionSet
Dim AcdEnty As AcadEntity


Dim FilterType(0) As Integer
Dim FilterData(0) As Variant


Set SelSet = MyDwg.SelectionSets.Add("MySS")
On Error Resume Next: MyDwg.SelectionSets.Item("MySS").Delete: On Error GoTo 0

FilterType(0) = 8
FilterData(0) = "Layer1"


SelSet.Select acSelectionSetAll, , , FilterType, FilterData

Debug.Print SelSet.Count

End Sub

/>

0 Likes
Message 4 of 7

MakCADD
Advocate
Advocate

You can use event handler to add text when you add a polyline 

 

0 Likes
Message 5 of 7

norman.yuan
Mentor
Mentor

Based on your description, it seems to me, what you want to do is to associate 2 entities (or to establish some kind of link between them). In your case, it is to have a text entity as a polyline's "label", so that when you export the polyline to external data storage (Excel, or anything else for that matter), not only is Handle and its geometry (points), but also the associated text entity information, both are saved. So, here what/where to export is not main concern here, but how to associate the 2 entities. so that your code can pick up one polyline and know if there is a companion text entity; if your code would need to add one (by itself, or by asking user interaction).

 

The viable/simple way to do it with COM API/VBA is to embed a piece of data to the target polyline or the text entity or both of them as either XData, or ExtensionDictionary (in this case, XData would be easier and sufficient). That is, you attach the text entity's handle to the polyline, or attach the polyline's handle to the text entity; or do it on both. 

 

If the polylines are the main entities in interest, I assume you should have certain criteria to identify them, such as by their layer. Then, to be simple, you can only attach polyline's handle to each text entity as XData. So, once you have all polyline identitied, you can use a SelectionSet to select all polyline-associated texts by using XData app name as selection filter, then loop through these texts for the XData to find out which polyline the text entity is linked to.

 

Hope this helps.

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 7

Kahva
Contributor
Contributor

Hello norman.yuan !

You perfectly decribed what I wanted, and described it better than myself 🙂

 

I opted to use xdata like you said. You proposed to use xdata app name as a selection filter, so if I understood correctly, the app name should be defined as a layer name, and the data will be filtered by layer?

 

In the documentation regarding xdata, it says to put NULL value for app data to return all values in xdata, does that mean I should put " " instead app name like this: .GetXData "", xtypeOut, xdataOut. 

 

Thank you for explanations!

Best regards!

0 Likes
Message 7 of 7

norman.yuan
Mentor
Mentor

Since an entity could have many different set of XData attached by different application/for different purpose, so the XData is grouped by "Application", a string value, thus, it cannot be empty string. Just give it a name meaningful to your purpose, such as "MyApp.AreaLabel". So, the XData to be attached to the Text entity would be like:

 

Dim dataTypes(0 to 1) As Integer
Dim dataValues(0 to 1) As Varaint
dataTypes[0]=1001 '' ExtendedDataApplicationName Group Code
dataValues(0)="MyApp.Label" '' ExtendedDataApplicationName value
dataType(1)=1000 : '' ExtendedData Type - ASCII Text
dataValue(1)=polylineEntity.Handle

textEnt.SetXData dataTypes, dataValues

 

Norman Yuan

Drive CAD With Code

EESignature