AutoCAD Civil 3D Customization

Reply
Valued Contributor
fallright
Posts: 75
Registered: ‎09-14-2009
Message 1 of 4 (191 Views)

read anonymous blocks

191 Views, 3 Replies
10-23-2012 03:54 AM

I have the follwoing error in C3D R2013.

 

Unable to cast object of type 'Autodesk.AutoCAD.DatabaseServices.BlockTableRecord' to type 'Autodesk.AutoCAD.DatabaseServices.BlockReference'.

When casting from a number, the value must be a number less than infinity

Make sure the source type is convertible to the destination type

 

My code is:

Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.LayerManager
Imports Autodesk.AutoCAD.Windows
Imports Autodesk.AutoCAD.PlottingServices
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.Civil.DatabaseServices
Imports Autodesk.Civil.ApplicationServices
Imports Autodesk.Civil.DatabaseServices.LabelGroup
Imports Autodesk.Civil.DatabaseServices.Styles.LabelSetStylesRoot
Imports Autodesk.Civil.Runtime
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.VisualBasic.FileIO.FileSystem
Imports System.IO
Imports System.Convert
Imports Microsoft.VisualBasic.Strings
Imports System.Runtime.InteropServices
Imports System.Collections.Specialized

...

Using tr As Transaction = db.TransactionManager.StartTransaction()
            Dim btable As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
            If btable.Has("vbredakdettek") Then
                Dim btr As BlockTableRecord = DirectCast(tr.GetObject(btable("vbredakdettek"), OpenMode.ForRead), BlockTableRecord)
                For Each refId As ObjectId In btr.GetAnonymousBlockIds()
                    myBlock = DirectCast(tr.GetObject(refId, OpenMode.ForRead), BlockReference)

...

 

*Expert Elite*
Alfred.NESWADBA
Posts: 8,867
Registered: ‎06-29-2007
Message 2 of 4 (189 Views)

Re: read anonymous blocks

10-23-2012 04:01 AM in reply to: fallright

Hi,

 

>> Unable to cast object of type 'Autodesk.AutoCAD.DatabaseServices.BlockTableRecord'

>> to type 'Autodesk.AutoCAD.DatabaseServices.BlockReference'.

I have not tried it, but I guess that the function .GetAnonymousBlockIds returns ObjectIDs of BlockTableRecords.

 

So change your last shown code line to:

      Dim myBlockBTR as BlockTableRecord = DirectCast(tr.GetObject(refId, OpenMode.ForRead), BlockTableRecord)

...does that work?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Valued Contributor
fallright
Posts: 75
Registered: ‎09-14-2009
Message 3 of 4 (183 Views)

Re: read anonymous blocks

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

that works, but my code continues with:

 

            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim btable As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                If btable.Has("vbredakdettek") Then
                    Dim btr As BlockTableRecord = DirectCast(tr.GetObject(btable("vbredakdettek"), OpenMode.ForRead), BlockTableRecord)
                For Each refId As ObjectId In btr.GetAnonymousBlockIds()
                    Dim myBlockBTR As BlockTableRecord = DirectCast(tr.GetObject(refId, OpenMode.ForRead), BlockTableRecord)
                    'myBlock = DirectCast(tr.GetObject(refId, OpenMode.ForRead), BlockReference)


                    Dim owner As BlockTableRecord = DirectCast(tr.GetObject(myBlock.OwnerId, OpenMode.ForRead), BlockTableRecord)
                    If owner.IsLayout Then
                        If myBlock.Name.ToUpper = "VBREDAKDETTEK" Then

 How can I then get "myblock" ?

*Expert Elite*
Alfred.NESWADBA
Posts: 8,867
Registered: ‎06-29-2007
Message 4 of 4 (173 Views)

Re: read anonymous blocks

10-23-2012 08:42 AM in reply to: fallright

Hi,

 

use the function myBlockBTR.GetBlockReferenceIDs to get the ObjectIDs pointing to the blockreferences then.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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 to get help installing your software.

Ask the Community


Civil 3D Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for AutoCAD Civil 3D helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Civil 3D

Twitter

Facebook

Blogs