Author Topic: Text from Layer  (Read 2525 times)

0 Members and 1 Guest are viewing this topic.

cadpro

  • Guest
Text from Layer
« on: October 26, 2011, 01:24:59 PM »
Hi,

I'd like to get the text entities in a particular layer. How would I do that please?

Thanks

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Text from Layer
« Reply #1 on: October 26, 2011, 01:45:49 PM »
You could have a look here:

http://www.theswamp.org/index.php?topic=31489.msg370261#msg370261

It describes a class which you can use to create a selection set and in that you can add layernames but also entity types. Ie you can easily get all green lines and red polylines on layer Defpoints.

The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

fixo

  • Guest
Re: Text from Layer
« Reply #2 on: October 26, 2011, 05:52:30 PM »
Try a bit easier:
Code: [Select]
        <CommandMethod("SELF")> _
        Public Sub TestSelectionFilter()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument()
            Dim ed As Editor = doc.Editor
            Try
                Dim pso As PromptSelectionOptions = New PromptSelectionOptions
                pso.MessageForAdding = Environment.NewLine & "Select objects : "
                Dim res As PromptSelectionResult
                Dim filterList(,) As Object = New Object(,) {{-4, "<and"}, {0, "Text"}, {8, "Layer1"}}'<-- change your layer here
                Dim tvs(filterList.GetUpperBound(0)) As TypedValue
                For i As Integer = 0 To filterList.GetUpperBound(0)
                    tvs(i) = New TypedValue(Convert.ToInt32(filterList(i, 0)), filterList(i, 1))
                Next
                Dim filt As SelectionFilter = New SelectionFilter(tvs)
                res = ed.GetSelection(pso, filt)
                If res.Status <> PromptStatus.OK Then
                    Return
                End If
                ed.WriteMessage(Environment.NewLine + "Selected texts: {0}", res.Value.Count)
                ''do your mojo here
            Catch exc As System.Exception
                ed.WriteMessage(exc.Message & Environment.NewLine & exc.StackTrace)
            Finally
            End Try
        End Sub

cadpro

  • Guest
Re: Text from Layer
« Reply #3 on: October 27, 2011, 02:57:01 PM »
Thanks huiz and fixo. I think I may have to change my logic. I will have to get the existing xrecord from entities. I will post a new thread on this topic. Thanks for your help!

Thanks

DogBone

  • Guest
Re: Text from Layer
« Reply #4 on: November 02, 2011, 11:47:05 AM »
I just did something like that. I didn't know how to use the filter yesterday, so thanks for that Fixo!

Code: [Select]
Public Sub RemoveGridText()
        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim acDb As Database = acDoc.Database
        Dim acEd As Editor = acDoc.Editor

        Using acTrans As Transaction = acDb.TransactionManager.StartTransaction
            'Open block table record for read
            Dim acBT As BlockTable = acDb.BlockTableId.GetObject(OpenMode.ForRead)
            'Open block table record paperspace for write
            Dim acBTR As BlockTableRecord = acTrans.GetObject(acBT(BlockTableRecord.PaperSpace), OpenMode.ForWrite)
            ' Place code to delete text with middle center attachment & layer G-ANNO-LEGEND
            For Each acID As ObjectId In acBTR
                'set a database object from the record object
                Dim acObj As DBObject = acID.GetObject(OpenMode.ForWrite)
                'If the database object is a block reference
                If TypeOf acObj Is MText Then
                    Dim acTxtObj As MText = DirectCast(acObj, MText)
                    If acTxtObj.Layer = "G-ANNO-LEGEND" And acTxtObj.Attachment = AttachmentPoint.MiddleCenter Then
                        acTxtObj.Erase(True)
                    End If
                End If
            Next
    acTrans.Commit()
End Using
  End Sub

fixo

  • Guest
Re: Text from Layer
« Reply #5 on: November 02, 2011, 12:04:33 PM »
You're welcome
Cheers :)

kaefer

  • Guest
Re: Text from Layer
« Reply #6 on: November 02, 2011, 02:00:52 PM »
I just did something like that.

I just wanted to know how my favourite evil extension would fare in VB.NET.
Does yours also do its magic in the first Layout when you're in ModelSpace?

Code: [Select]
Option Strict On
Imports System.Linq
Imports System.Runtime.CompilerServices
Public Class RemoveGridText
    <CommandMethod("RemoveGridText")> _
    Public Sub RemoveGridTextExtCmd()
        Dim db = Application.DocumentManager.MdiActiveDocument.Database
        Dim ed = Application.DocumentManager.MdiActiveDocument.Editor
        Using tr As Transaction = db.TransactionManager.StartTransaction()
            For Each mt In
                From btr In db.BlockTableId _
                    .GetObject(Of BlockTable)() _
                    .GetObjects(Of BlockTableRecord)()
                Where SymbolUtilityServices.IsBlockPaperSpaceName(btr.Name)
                From tx In btr.GetObjects(Of MText)()
                Where tx.Layer = "G-ANNO-LEGEND" AndAlso tx.Attachment = AttachmentPoint.MiddleCenter
                Select tx
                mt.UpgradeOpen()
                mt.Erase()
            Next
            tr.Commit()
        End Using
    End Sub
End Class
Module Extensions
    <Extension()>
    Public Function GetObject(Of T As {DBObject})(id As ObjectId) As T
        GetObject =
            TryCast(id.GetObject(OpenMode.ForRead), T)
    End Function
    <Extension()>
    Public Function GetObjects(Of T As {DBObject})(ids As IEnumerable) As IEnumerable(Of T)
        GetObjects =
            From id In ids.Cast(Of ObjectId)()
            Select res = id.GetObject(Of T)()
            Where res IsNot Nothing
    End Function
End Module

DogBone

  • Guest
Re: Text from Layer
« Reply #7 on: November 03, 2011, 09:41:09 AM »

I just wanted to know how my favourite evil extension would fare in VB.NET.
Does yours also do its magic in the first Layout when you're in ModelSpace?

Code: [Select]
...
            For Each mt In
                From btr In db.BlockTableId _
                    .GetObject(Of BlockTable)() _
                    .GetObjects(Of BlockTableRecord)()
                Where SymbolUtilityServices.IsBlockPaperSpaceName(btr.Name)
                From tx In btr.GetObjects(Of MText)()
...

By the time my program hits that text delete part it's already in paperspace, but that's a good thought, I will try that.
I tried using your code in VB, but when it get's to the code snippet above, it doesn't like the imbedded style. In VB when you use GetObject it wants to know what mode. I want to spend more time with your code to see if it will still do it,
by adding a little mode in your code.   :lmao:

Again, I do like the C style, it's more like using precedents in algebra and that may be where we can add the mode.
I dunno, I haven't tried this yet.

Thanks, I'm going to learn a lot from you guys, in a short amount of time.   ;-)

cadpro

  • Guest
Re: Text from Layer
« Reply #8 on: November 03, 2011, 10:48:49 AM »
Thanks guys. That's solved!

Thanks

kaefer

  • Guest
Re: Text from Layer
« Reply #9 on: November 03, 2011, 11:20:07 AM »
In VB when you use GetObject it wants to know what mode.

The signature of Autodesk.AutoCAD.DatabaseServices.ObjectId.GetObject says so. To be able to call GetObject in fluent style I need it to do the cast to the target type. That's why I extended the ObjectId struct with a GetObject method of my own.

Another potential source of confusion is the OpenMode enum from the Microsoft.VisualBasic namespace, which I resolved not to import.

DogBone

  • Guest
Re: Text from Layer
« Reply #10 on: November 03, 2011, 03:00:01 PM »
... To be able to call GetObject in fluent style I need it to do the cast to the target type. That's why I extended the ObjectId struct with a GetObject method of my own.

Ah...I'm catching on.  :kewl: