AutoCAD Map 3D Developer

AutoCAD Map 3D Developer

Reply
Contributor
khushboo.singh
Posts: 20
Registered: ‎11-09-2011
Message 21 of 30 (153 Views)

Re: Delete Polyline for Selected Objects

10-19-2012 05:53 AM in reply to: Alfred.NESWADBA

Could you please tell me what code changes should I make?

I have used tried to check whether the object is AcDbPolyline or not.

But still it does not server the purpose.

Could you please be a little more specific in code perspective?

*Expert Elite*
Alfred.NESWADBA
Posts: 9,598
Registered: ‎06-29-2007
Message 22 of 30 (150 Views)

Re: Delete Polyline for Selected Objects

10-19-2012 09:11 AM in reply to: khushboo.singh

Hi,

 

I hope this helps, it shows how to filter to any type of  polylines during selection and then erase the selected items.

   <Autodesk.AutoCAD.Runtime.CommandMethod("ADESK_erasePoly", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)> _
   Public Shared Sub erasePoly()
      Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
      Dim acDoc As Document = acDocMgr.MdiActiveDocument

      'define a filter for objecttypes to be selectable
      'we define now "*POLY*" so we select only 2D-Poly, 3D-Poly and LW-Poly
      Dim tFilter As SelectionFilter = New SelectionFilter(New TypedValue() _
                                        {New TypedValue(DxfCode.Start, "*POLY*")})

      'Get the selected objects in the drawing area
      Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection(tFilter)
      'If the prompt Status is OK, the objects are selected
      If acSSPrompt.Status = PromptStatus.OK Then

         Dim tDocLock As Autodesk.AutoCAD.ApplicationServices.DocumentLock = Nothing
         Dim tTrAct As Transaction = Nothing
         Try
            tDocLock = acDoc.LockDocument
            tTrAct = acDoc.Database.TransactionManager.StartTransaction

            For Each tObjID As ObjectId In acSSPrompt.Value.GetObjectIds
               If (tObjID.IsValid) AndAlso (Not tObjID.IsErased) Then
                  'just to make sure, the object is valid
                  tTrAct.GetObject(tObjID, OpenMode.ForWrite).Erase()
               End If
            Next

            tTrAct.Commit()
         Catch ex As Exception
            Call MsgBox("Error occured." & vbNewLine & ex.Message)
         Finally
            If tTrAct IsNot Nothing Then tTrAct.Dispose() : tTrAct = Nothing
            If tDocLock IsNot Nothing Then tDocLock.Dispose() : tDocLock = Nothing
         End Try

      End If
   End Sub

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Contributor
khushboo.singh
Posts: 20
Registered: ‎11-09-2011
Message 23 of 30 (140 Views)

Re: Delete Polyline for Selected Objects

10-22-2012 08:05 PM in reply to: Alfred.NESWADBA

Thanks a lot!!!
It worked, I am able to delete the polyline.

I could understand the mistakes that I was doing in the code.

 

I am attaching here a doc which mentions the process I am stuck in.

The dwg is a structure of a floor which contains space details in the form of cubes.

When I am trying to select a polyline and delete it, it is getting erased, but the text inside the polyline remains as it is.

I opened the Properties section to check what contains the text, I got there are two sections: Text & OD:smileyvery-happy:BLINK.

As far as I can understand if I am able to retrieve Text and delete it, I can delete the text also from dwg.

I am able to retrieve OD:smileyvery-happy:BLINK as:

Dim dbObj As DBObject = tTrAct.GetObject(id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)

odTable = odTables.Item("DBLINK")

Dim odrecords As ObjectData.Records = odTable.GetObjectTableRecords(Convert.ToUInt32(0), id, Constants.OpenMode.OpenForWrite, True)

MsgBox(odrecords(0).Item(0).StrValue) - This shows me the value KSOPHJ022B352

 

But I do not know how to retrieve the Text column from the properties file and delete it.

 

Please let me know if my concept is wrong somewhere.

*Expert Elite*
Alfred.NESWADBA
Posts: 9,598
Registered: ‎06-29-2007
Message 24 of 30 (135 Views)

Re: Delete Polyline for Selected Objects

10-23-2012 12:46 AM in reply to: khushboo.singh

Hi,

 

can you upload a drawing with some of the geometry (holding object-data).

As I don't know how the object-data is structured I can't tell you now if you can find the text by od.

 

As long as the structure is as clean as you showed in the screenshots it is also easy to find the text inside the polyline-rectangles as you only have to scan through all the text-objects and look which mid-point of the text is inside a specific rectangle.

An option may also be to use a selectionset with option "WindowPolygon" using the vertices if the rectangle, but in this case you need to have the text visible on screen (at least within the current REGEN-area, otherwise the selection on screen (to be used with WindowPolygon) can't find the text.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Contributor
khushboo.singh
Posts: 20
Registered: ‎11-09-2011
Message 25 of 30 (133 Views)

Re: Delete Polyline for Selected Objects

10-23-2012 01:43 AM in reply to: Alfred.NESWADBA

I have attached here the dwg file.

Let me know if that is sufficient to get the Text or do I need to provide anything else.

*Expert Elite*
Alfred.NESWADBA
Posts: 9,598
Registered: ‎06-29-2007
Message 26 of 30 (130 Views)

Re: Delete Polyline for Selected Objects

10-23-2012 01:56 AM in reply to: khushboo.singh

Hi,

 

thx for the drawing + the doc, makes it quite easier to understand for me.

A last question to make sure that I don't start into the wron direction. You stated.

>> But I do not know how to retrieve the Text column from the properties file and delete it.

Do you mean "how to delete the text-object from the drawing"? Or do you look for a way to erase a object-data-record when you deleted an object (your polyline)?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Contributor
khushboo.singh
Posts: 20
Registered: ‎11-09-2011
Message 27 of 30 (128 Views)

Re: Delete Polyline for Selected Objects

10-23-2012 02:19 AM in reply to: Alfred.NESWADBA

As I have displayed in the doc, when I select the text object polyline, that gets deleted, but the text remains the same.

What I want, when I select the polyline and delete it, the associated text also should be deleted from the dwg.

 

For that what I understood, if I am able to retrieve the Text from the Properties and I am able to delete it, it would delete the text from the dwg as well.

 

I am not sure about the difference between deleting only the text from the dwg and deleting object-data-record.

 

Deleting only the text - does that mean the text would disapear from the dwg but if I click on the properties window for the text, the text would still appear on the properties window?

 

Deleting object-data-records - the text as well as entry from the properties window, both would be lost?

 

Is my understanding correct?

*Expert Elite*
Alfred.NESWADBA
Posts: 9,598
Registered: ‎06-29-2007
Message 28 of 30 (124 Views)

Re: Delete Polyline for Selected Objects

10-23-2012 03:08 AM in reply to: khushboo.singh

Hi,

 

>> I am not sure about the difference between deleting only the text from the dwg and deleting object-data-record.

The OD-record is not the same as a record in the database and it can't exist without an entity that has the record assigned.

So if you delete the OD-record, the Map-ObjectData is removed from the geometry-object.

If you delete the DBText from the drawing then the OD-record is gone also (as it can't exist without an object it's assigned to)

 

>> Deleting only the text - does that mean the text would disapear from the dwg

>> but if I click on the properties window for the text, the text would still appear

>> on the properties window?

Now we have to make sure what wording is used (soory I'm not english-native speaking)

One is the geomtry-object TEXT (or DatabaseServices.DBText), if you delete it (command _ERASE and click onto the object) then it's gone from the drawing.

The other option is the content if the text-object (the string). If you select the text-object and remove the characters from the content of the geometry object ==> then yes, the geometry object stays in the drawing, but it's not visible (as there are no more characters to be displayed) and so it's also not selectable. And to be honest: this would be the worst way to remove text from a drawing, because the drawing-size is high even if you don't see any objects. I would suggest never to use this way.

 

I hope that makes it clear now (and I have described it in a readable way).


You have now four options (imho):

 

1)

  • when selecting the polyline, read it's content from the OD-record to get the ID, then erase the polyline
  • from the OD-record you can get the string to search a textobject that has this id (last characters)
  • search for the text and erase it from the database

2)

  • when selecting the polyline, read it's content from the OD-record to get the ID, then erase the polyline
  • scan through all elements having OD-records from the same table assigned
  • if the scaned object is of type text and the ID is the same as that from the erase poly ==> erase the text and end the loop

3)

  • when selecting the polyline get it's extents
  • scan through all text-objects on that layer and verify if the extents of the text is inside the extents of the erased poly
  • if so erase the text-object and exit the loop

4)

  • when selecting the polyline get it's extents
  • zoom to the extents (and then ZOOM 0.8xp)
  • create a selectionset with Window (or WindowPolygon) and use the extents (or the vertices of the poly) + a filter for text-objects on that specific layer (if the layer is constant)
  • if found (and only one found) then erase the object otherwise report an error

 

1) is a stable way and should be quite fast/needs Map to work

2) is a stable way, not so fast as you have to scan for objects having the correct ID in the OD-records/needs Map to work

3) is critical as it might find other text-objects then. Ff the structure is as clear as your sample drawing, than no problem, but if there are more textobjects within this polyline, and maybe on the same layer, it could get problematic. But it does not need Map (if that is an advantage).

4) same critical as 3 + you have to make sure that the text-objects is on screen before creating a selection. That may sound slower first. But if you erase the polyline by manual selection then you don't need the zoom as you are alread there. Depends on the needs. Needs no Map.

 

HTH, - alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Contributor
khushboo.singh
Posts: 20
Registered: ‎11-09-2011
Message 29 of 30 (121 Views)

Re: Delete Polyline for Selected Objects

10-23-2012 06:24 AM in reply to: Alfred.NESWADBA

Thanks a lot for the detailed description, it would clear so many concepts in one go.

 

Here I am pasting the code where I have selected the objects on the dwg files as -

        Dim tFilter As SelectionFilter = New SelectionFilter(New TypedValue() _
                                          {New TypedValue(DxfCode.Start, "*POLY*")})

 

Once selected I am deleting the polyline.

But before that I am trying to get the OD:smileyvery-happy:BLINK, I am getting the value but not able to delete it.

In the same way, could you please guide me the code how can I scan for the value "Text" in the properties window?

The value for "Text" is fixed for one rectangle-polyline, but different for all the rectangle-polylines.

 

            Dim tDocLock As Autodesk.AutoCAD.ApplicationServices.DocumentLock = Nothing
            Dim tTrAct As Transaction = Nothing
            Dim odTable As Autodesk.Gis.Map.ObjectData.Table
            Dim odTables As Autodesk.Gis.Map.ObjectData.Tables
            Dim hs As HostApplicationServices = HostApplicationServices.Current
            Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
            odTables = HostMapApplicationServices.Application.ActiveProject.ODTables
            Try
                tDocLock = acDoc.LockDocument
                tTrAct = acDoc.Database.TransactionManager.StartTransaction

                For Each tObjID As ObjectId In acSSPrompt.Value.GetObjectIds
                    If (tObjID.IsValid) AndAlso (Not tObjID.IsErased) Then
                        'just to make sure, the object is valid
                        acObjIdColl.Add(tObjID)
                        For Each id As ObjectId In acObjIdColl
                            'Dim tCurve As Curve = TryCast(tTrAct.GetObject(id, OpenMode.ForWrite), Curve)
                            Dim dbObj As DBObject = tTrAct.GetObject(id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
                            odTable = odTables.Item("DBLINK")
                            Dim odrecords As ObjectData.Records = odTable.GetObjectTableRecords(Convert.ToUInt32(0), id, Constants.OpenMode.OpenForWrite, True)
                            MsgBox(odrecords(0).Item(0).StrValue)
                        Next
                        tTrAct.GetObject(tObjID, OpenMode.ForWrite).Erase()
                    End If
                Next

*Expert Elite*
Alfred.NESWADBA
Posts: 9,598
Registered: ‎06-29-2007
Message 30 of 30 (113 Views)

Re: Delete Polyline for Selected Objects

10-24-2012 12:38 AM in reply to: khushboo.singh

Hi,

 

>> code how can I scan for the value "Text" in the properties window?

Does that help?

<Autodesk.AutoCAD.Runtime.CommandMethod("ADESK_eraseText")> _
Public Shared Sub eraseText()
   Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
   Dim acDoc As Document = acDocMgr.MdiActiveDocument

   Dim tSearchStr As String = "1C109"		'take your string here

   'define a filter for objecttypes to be selectable
   'we define now "TEXT" as objecttype to search and the content/value of the text
   'and define only to search in model-space
   Dim tFilter As SelectionFilter = New SelectionFilter(New TypedValue() _
													 {New TypedValue(DxfCode.Start, "*TEXT*"), _
													  New TypedValue(DxfCode.Text, tSearchStr), _
													  New TypedValue(DxfCode.LayoutName, "Model")})

   'now get the selection
   Dim tSelSetRes As PromptSelectionResult = acDoc.Editor.SelectAll(tFilter)
   If (tSelSetRes IsNot Nothing) AndAlso (tSelSetRes.Status = PromptStatus.OK) AndAlso (tSelSetRes.Value.Count = 1) Then
	Dim tTrAct As Transaction = Nothing
	Try
		tTrAct = acDoc.TransactionManager.StartTransaction
		Dim tObjID As ObjectId = tSelSetRes.Value.GetObjectIds(0)
		Dim tTxtEnt As DBText = CType(tTrAct.GetObject(tObjID, OpenMode.ForWrite), DBText)
		tTxtEnt.Erase()
		tTrAct.Commit()
	Catch ex As Exception
		'something happend
		MsgBox("Unknown error: " & ex.Message & vbNewLine & ex.StackTrace)
	Finally
		If tTrAct IsNot Nothing Then tTrAct.Dispose() : tTrAct = Nothing
	End Try
  Else
	'send message to user
	MsgBox("no DBText (or more than one) with content " & tSearchStr & " found in ModelSpace")
  End If

End Sub

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.