.NET

Reply
Active Contributor
xdbk07
Posts: 42
Registered: ‎11-06-2012
Message 1 of 5 (1,137 Views)
Accepted Solution

Copy block reference "same autocad" and change att value

1137 Views, 4 Replies
02-26-2013 09:01 PM

Hi All,

 

Please help me to creat a copy of block definition like autocad (block display beside mouse when we move) and replace some text of attribute.

 

Thanks,

I want copy a block and replace some text string of attribute definition. Here is Kean 's code:

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Internal
Namespace BlockJigTest
Class BlockJig
Inherits EntityJig
Private mCenterPt As Point3d, mActualPoint As Point3d
Public Sub New(br As BlockReference)
MyBase.New(br)
mCenterPt = br.Position
End Sub
Protected Overloads Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus
Dim jigOpts As New JigPromptPointOptions()
jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates Or UserInputControls.NoZeroResponseAccepted Or UserInputControls.NoNegativeResponseAccepted)
jigOpts.Message = vbLf & "Enter insert point: "
Dim dres As PromptPointResult = prompts.AcquirePoint(jigOpts)
If mActualPoint = dres.Value Then
Return SamplerStatus.NoChange
Else
mActualPoint = dres.Value
End If
Return SamplerStatus.OK
End Function
Protected Overloads Overrides Function Update() As Boolean
mCenterPt = mActualPoint
Try
DirectCast(Entity, BlockReference).Position = mCenterPt
Catch generatedExceptionName As System.Exception
Return False
End Try
Return True
End Function
Public Function GetEntity() As Entity
Return Entity
End Function
End Class
Public Class Commands
<CommandMethod("BJIG")> _
Public Sub CreateBlockWithJig()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
' First let's get the name of the block
Dim opts As New PromptStringOptions(vbLf & "Enter block name: ")
Dim pr As PromptResult = ed.GetString(opts)
If pr.Status = PromptStatus.OK Then
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
' Then open the block table and check the
' block definition exists
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
If Not bt.Has(pr.StringResult) Then
ed.WriteMessage(vbLf & "Block not found.")
Else
Dim bdId As ObjectId = bt(pr.StringResult)
' We loop until the jig is cancelled
While pr.Status = PromptStatus.OK
' Create the block reference and
' add it to the jig
Dim pt As New Point3d(0, 0, 0)
Dim br As New BlockReference(pt, bdId)
' Start annot-scale support code
Dim bd As BlockTableRecord = DirectCast(tr.GetObject(bdId, OpenMode.ForRead), BlockTableRecord)
' Using will dispose of the block definition
' when no longer needed
Using bd
If bd.Annotative = AnnotativeStates.[True] Then
Dim ocm As ObjectContextManager = db.ObjectContextManager
Dim occ As ObjectContextCollection = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES")
ObjectContexts.AddContext(br, occ.CurrentContext)
End If
End Using
' End annot-scale support code
Dim entJig As New BlockJig(br)
' Perform the jig operation
pr = ed.Drag(entJig)
If pr.Status = PromptStatus.OK Then
' If all is OK, let's go and add the
' entity to the modelspace
Dim ms As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
ms.AppendEntity(entJig.GetEntity())
tr.AddNewlyCreatedDBObject(entJig.GetEntity(), True)
' Start attribute support code
bd = DirectCast(tr.GetObject(bdId, OpenMode.ForRead), BlockTableRecord)
' Add the attributes
For Each attId As ObjectId In bd
Dim ent As Entity = DirectCast(tr.GetObject(attId, OpenMode.ForRead), Entity)
If TypeOf ent Is AttributeDefinition Then
Dim ad As AttributeDefinition = DirectCast(ent, AttributeDefinition)
Dim ar As New AttributeReference()
ar.SetAttributeFromBlock(ad, br.BlockTransform)
br.AttributeCollection.AppendAttribute(ar)
tr.AddNewlyCreatedDBObject(ar, True)
End If
Next
' End attribute support code
' Call a function to make the graphics display
' (otherwise it will only do so when we Commit)
doc.TransactionManager.QueueForGraphicsFlush()
End If
End While
End If
tr.Commit()
End Using
End If
End Sub
End Class
End Namespace

 It work OK but it is not what i need. I want to get the block by click it and edit textstring of attribute. It's really hard for me to do that. Please help me.

 

Thanks,

Moderator
Alexander.Rivilis
Posts: 1,449
Registered: ‎04-09-2008
Message 2 of 5 (1,116 Views)

Re: Copy block reference "same autocad" and change att value

02-27-2013 12:58 AM in reply to: xdbk07

Using a jig from .NET to multiply insert AutoCAD blocks

Using a jig from .NET to multiply insert AutoCAD blocks - Part 2

Jigging an AutoCAD block with attributes using .NET


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
xdbk07
Posts: 42
Registered: ‎11-06-2012
Message 3 of 5 (1,094 Views)

Re: Copy block reference "same autocad" and change att value

02-27-2013 06:52 PM in reply to: xdbk07

Thank for help. These code are really complex.

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 4 of 5 (1,089 Views)

Re: Copy block reference "same autocad" and change att value

02-27-2013 07:29 PM in reply to: xdbk07

Your subject says copy block reference, but your message says copy block definition.

 

Which is it? 

 

The DeepCloneObjects() method of the Database class is used to copy objects, so that's what you'd use for copying a block reference. Copying a block definition is a bit more complicated, but also involves the use of DeepCloneObjects()..

Active Contributor
xdbk07
Posts: 42
Registered: ‎11-06-2012
Message 5 of 5 (1,083 Views)

Re: Copy block reference "same autocad" and change att value

02-27-2013 08:22 PM in reply to: DiningPhilosopher

I want copy a block and replace some text string of attribute definition. Here is Kean 's code:

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Internal
Namespace BlockJigTest
    Class BlockJig
        Inherits EntityJig
        Private mCenterPt As Point3d, mActualPoint As Point3d
        Public Sub New(br As BlockReference)
            MyBase.New(br)
            mCenterPt = br.Position
        End Sub
        Protected Overloads Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus
            Dim jigOpts As New JigPromptPointOptions()
            jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates Or UserInputControls.NoZeroResponseAccepted Or UserInputControls.NoNegativeResponseAccepted)
            jigOpts.Message = vbLf & "Enter insert point: "
            Dim dres As PromptPointResult = prompts.AcquirePoint(jigOpts)
            If mActualPoint = dres.Value Then
                Return SamplerStatus.NoChange
            Else
                mActualPoint = dres.Value
            End If
            Return SamplerStatus.OK
        End Function
        Protected Overloads Overrides Function Update() As Boolean
            mCenterPt = mActualPoint
            Try
                DirectCast(Entity, BlockReference).Position = mCenterPt
            Catch generatedExceptionName As System.Exception
                Return False
            End Try
            Return True
        End Function
        Public Function GetEntity() As Entity
            Return Entity
        End Function
    End Class
    Public Class Commands
        <CommandMethod("BJIG")> _
        Public Sub CreateBlockWithJig()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            ' First let's get the name of the block
            Dim opts As New PromptStringOptions(vbLf & "Enter block name: ")
            Dim pr As PromptResult = ed.GetString(opts)
            If pr.Status = PromptStatus.OK Then
                Dim tr As Transaction = doc.TransactionManager.StartTransaction()
                Using tr
                    ' Then open the block table and check the
                    ' block definition exists
                    Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                    If Not bt.Has(pr.StringResult) Then
                        ed.WriteMessage(vbLf & "Block not found.")
                    Else
                        Dim bdId As ObjectId = bt(pr.StringResult)
                        ' We loop until the jig is cancelled
                        While pr.Status = PromptStatus.OK
                            ' Create the block reference and
                            ' add it to the jig
                            Dim pt As New Point3d(0, 0, 0)
                            Dim br As New BlockReference(pt, bdId)
                            ' Start annot-scale support code
                            Dim bd As BlockTableRecord = DirectCast(tr.GetObject(bdId, OpenMode.ForRead), BlockTableRecord)
                            ' Using will dispose of the block definition
                            ' when no longer needed
                            Using bd
                                If bd.Annotative = AnnotativeStates.[True] Then
                                    Dim ocm As ObjectContextManager = db.ObjectContextManager
                                    Dim occ As ObjectContextCollection = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES")
                                    ObjectContexts.AddContext(br, occ.CurrentContext)
                                End If
                            End Using
                            ' End annot-scale support code
                            Dim entJig As New BlockJig(br)
                            ' Perform the jig operation
                            pr = ed.Drag(entJig)
                            If pr.Status = PromptStatus.OK Then
                                ' If all is OK, let's go and add the
                                ' entity to the modelspace
                                Dim ms As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
                                ms.AppendEntity(entJig.GetEntity())
                                tr.AddNewlyCreatedDBObject(entJig.GetEntity(), True)
                                ' Start attribute support code
                                bd = DirectCast(tr.GetObject(bdId, OpenMode.ForRead), BlockTableRecord)
                                ' Add the attributes
                                For Each attId As ObjectId In bd
                                    Dim ent As Entity = DirectCast(tr.GetObject(attId, OpenMode.ForRead), Entity)
                                    If TypeOf ent Is AttributeDefinition Then
                                        Dim ad As AttributeDefinition = DirectCast(ent, AttributeDefinition)
                                        Dim ar As New AttributeReference()
                                        ar.SetAttributeFromBlock(ad, br.BlockTransform)
                                        br.AttributeCollection.AppendAttribute(ar)
                                        tr.AddNewlyCreatedDBObject(ar, True)
                                    End If
                                Next
                                ' End attribute support code
                                ' Call a function to make the graphics display
                                ' (otherwise it will only do so when we Commit)
                                doc.TransactionManager.QueueForGraphicsFlush()
                            End If
                        End While
                    End If
                    tr.Commit()
                End Using
            End If
        End Sub
    End Class
End Namespace

 It work OK but it is not what i need. I want to get the block by click it and edit textstring of attribute. It's really hard for me to do that. Please help me.

 

Thanks,

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!