Question

Automating AutoCAD - get block attributes objectdbx method...

Asked by: LittleOleMeDesigns

I am trying to read block attributes out of autocad with the objectdbx method.  I've it working successfully to a point.. in my code below, I'm connected to AutoCAD, I've opened the drawing, and I'm reading the blocks.

It's reading the blocks and putting them in the datagridview, but where there are two of a block in the file, it's only picking up one of them.  

Also, how can I can get custom Attributes of the block?

Dim Block As AXAUTO15Lib.AcadBlock
            For Each Block In V_AutoCadInterfaceObject.Blocks
                If Block.Name.ToString = "START" Then
 
                    DataGridView1.Rows.Add()
                    DataGridView1.Rows(CurrentDataGridView1Row).Cells(0).Value = Block.Name.ToString
                    CurrentDataGridView1Row = CurrentDataGridView1Row + 1
 
                End If
 
            Next Block

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-04-08 at 12:27:57ID24307097
Tags

AutoCAD

,

VB.Net

,

Visual Basic

Topics

AutoCAD Software

,

.NET

,

Visual Basic Programming

Participating Experts
2
Points
500
Comments
13

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Visual Basic and Autocad
    I am trying to learn to write programs that will call Autocad and perform some autocad operations. The Getobject call does not work but Createobject will when run from Visual basic. When compiled the both crateobject and Getobject causes autocad to hang.
  2. How to Extract Drawing Attributes from AutoCAD into a Tab…
    What I'm trying to acheive is to be able to extract drawing attribute information from AutoCAD and then fill out the necessary fields in a form. Is this possible.?? Another possible way would be to retreive similar information from a text file. For this to work though I w...
  3. AutoCad Properties
    Hi I am trying to access the file properties of an autocad dwg, but autocad installs its own file attributes section. When you right click it you see two summary pages a custom page and statistics. Is there anyway to access this information from vb .net Thanks Adam
  4. Attributes and data entry autocad 14
    Well after not working for 10 years and my kids are not living at home, I decided it to think about a job as a cad operator. Well somehow some head hunter found my resume on line and called me about a position that was open. Well I informed him that after 10 years not using a...
  5. Scaling in AutoCAD
    I have a floor plan whose dimensions I want to scale in one direction only. Compare it with a rectangle in Word, whose length can be increased by dragging on its edges in the horizontal direction. Is such an option available in AutoCAD

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: borgunitPosted on 2009-04-08 at 12:38:33ID: 24100924

I pulled this one out of my archives. It should get the block atts into a variant array. You will need to massage this a bit to update for .net

Private Function QueryBlkAttsInDwgMspace(ByVal sDwgFile As String, _
ByVal sBlkName As String) As Variant
'------------------------------------------------------------------------------
'QueryBlkAttsInDrawing:
'------------------------------------------------------------------------------
Dim dwgFile As String
Dim layer As AcadLayer
Dim acItem As AcadObject
Dim AcBlk As AcadBlockReference
Dim vAtts As Variant
Dim acAtt As AcadObject
Dim intCnt As Integer
Dim arrColumn()
'''''''''''''''''''''''''''''''''''''''
If sDwgFile <> "" Then
    'Set mobjdbx = AcadApplication.GetInterfaceObject("ObjectDBX.AxDbDocument")
    mObjDbx.Open sDwgFile
 
    If Err.Number <> 0 Then
        If Err.Number <> -2147467259 Then 'Invalid filename
            regObjectDBX
            mObjDbx.Open sDwgFile
        End If
    End If
 
    '--------------------------------------------------------------------------
    ' Check through the blocks in the drawing
    '--------------------------------------------------------------------------
    For Each acItem In mObjDbx.ModelSpace
        '----------------------------------------------------------------------
        ' If we find a block, check its name against the list of Grob titleblks
        '----------------------------------------------------------------------
        If TypeName(acItem) = "IAcadBlockReference" Then
            Set AcBlk = acItem
            '------------------------------------------------------------------
            ' If we find a match to the block name
            '------------------------------------------------------------------
            If AcBlk.name = sBlkName Then
                vAtts = AcBlk.GetAttributes
                QueryBlkAttsInDwgMspace = vAtts
                Exit Function
            End If
        End If
    Next acItem
End If
 
End Function

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:

Select allOpen in new window

 

by: LittleOleMeDesignsPosted on 2009-04-09 at 05:20:30ID: 24106479

Maybe I'm just not awake yet... but I can't make this work.

I think I'm getting mixed up on defining the variables somewhere when I try to integrate what you've got into my program... but I haven't figured out where yet.  I'm going to keep trying though.  This looks like it should work if I can get my head around it enough to integrate into mine.

May be that I'm missing something on defining the variables when I convert it to .NET too... I know vb6 Variant has to be Object, and because of something of with AutoCAD in some cases, Integer has to be Int16, but I don't know of anything else that should need converted.

I'll keep playing with it and see what I can do though, I'm going to leave this question open for now just in case I get completely stuck.  Thank-You for your help.

 

by: darrenmcwiPosted on 2009-04-09 at 11:25:08ID: 24110019

I've not gotten deep into .Net yet from Lisp and VB but from whst I saw in your code, it looks like you're querying the Block table for the blocks. The Block table contains the block definitions which would only have 1 instance. The Block table is what get's purged when the block in the table is not used.

You'll want to look for INSERT objects in the drawing which are inserted instances of the block. That's also where you can then extract the attributes,

 

by: LittleOleMeDesignsPosted on 2009-04-10 at 05:42:40ID: 24115097

Is there any chance you could explain that in more detail... how to look for INSERT objects in the drawing?  I tried googling it, and I'm not finding a good example.

 

by: borgunitPosted on 2009-04-10 at 06:04:41ID: 24115239

Once you have a reference to the objectDBX drawing file and have opened it (this is what my snippet references as mObjDbx), you would next have to cycle through all the objects in modelspace (if that is where the blockreference is, otherwise cycle through the objects in layoutSpace). The snippet picks this up at line 29. It goes through each item in the space you are searching and "checks" what it is "TypeName(acItem)". If it is a blockReference then it checks its name to see if it matches the on you are looking for. If it does, the "get" its attributes and set them to a variable. The code does not go any  further than that. Once you have the attributes set, you would need to go through the attributes and "pull" what you want. I hope this is making sense.

    Darrenmcwi is referring to your original code where you show cycling through each block in the block table. This is not the same as a blockreference. A block in AutoCAD can refer to modelspace and its entities, layoutspace and its entities, and all block definitions in the drawing. Blocks can be defined in a drawing but do not have to exist in the drawing. The block table is the database (you might say) where the drawing stores all the data it might need. A blockreference is an instance of a certain block that has been "inserted" into the drawing (it is what we see in the drawing). This is what you need to query, not the block. You need to cycle through all the entites in the drawings modelspace until you come upon a blockreference, once you do you check its name and then as I said above, do with it as you like from there.

 

by: LittleOleMeDesignsPosted on 2009-04-10 at 06:12:38ID: 24115284

@borgunit:

Thank-You for the explanation.  That makes a bit more sense.  I think I'm getting closer.. but it's not returning anything.  When I step through the code, it never goes inside the loop that says:

If TypeOf objEnt is AutoCAD.AcadBlockReference Then.....

I also tried:

If TypeName(objEnt) = "IAcadBlockReference" Then...

            Dim strBlkName As String
            Dim strAttTag As String
            Dim objEnt As AutoCAD.AcadEntity
            Dim objBlkRef As AutoCAD.AcadBlockReference
            Dim varAtts As Object
            Dim objAtt As AutoCAD.AcadAttributeReference
            Dim objLayout As AutoCAD.AcadLayout
            Dim objBlock As AutoCAD.AcadBlock
            Dim I As Long
 
            strBlkName = "START"
            strAttTag = "FILENAME"
            For Each objLayout In V_AutoCad.ActiveDocument.Layouts
                If UCase(objLayout.Name) <> "MODEL" Then
                    objBlock = objLayout.Block
                    For Each objEnt In objBlock
                        If TypeOf objEnt Is AutoCAD.AcadBlockReference Then
                            'If TypeName(objEnt) = "IAcadBlockReference" Then
 
                            objBlkRef = objEnt
                            If objBlkRef.Name = strBlkName Then
                                varAtts = objBlkRef.GetAttributes
                                For I = 0 To UBound(varAtts)
                                    objAtt = (varAtts(I))
                                    If objAtt.TagString = strAttTag Then
                                        MsgBox(objAtt.TextString)
                                    End If
                                Next I
                            End If
                        End If
                    Next
                End If
            Next

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:

Select allOpen in new window

 

by: darrenmcwiPosted on 2009-04-11 at 00:29:48ID: 24121129

Sorry, Borgunit explained it a little better. From LISP, you'll look for an INSERT entity type which is the data type returned in DXF format from Lisp. The BlockReference is thr same thing but in VB terminology.

It looks like you're trying to loop through the objects within the Layout block definition. Instead, you need to look through the object in the PaperSapce collection,. Look at BorgUnit's code and how it's iterating through the items in the ModelSpace collection. Modify that to go through the PaperSpace collection.

Take a peek at the developer guide, it shows the object model and it's structure which will givef you a better idea how to get to those items.

I'll see if I can't mod your code Monday back at the office if Borg doesn't already beat me to it.

 

by: LittleOleMeDesignsPosted on 2009-04-11 at 07:00:37ID: 24122072

@darrenmcwi:

I've been looking through the developer's guide.. and I can't seem to make heads or tails of how to do this.  Normally I don't have this much trouble grasping things... but it's like I've got "programmer's block" or something when it comes to this.

I'll keep hammering away at it and see if I can figure it out... I'll post back if I manage it.  If you could modify my code that would be wonderful!!  Maybe if I actually see what I'm doing made right then it'll sink out how to do it.

I've basically got about 10 blocks that I'm hunting for, and then I'm looking for specific attributes in those blocks.  For example:

Block Named "START" I need to get:
x insertion point
y insertion point
x scale
"SHEET"
"TITLE"

Block Named "t200" I need to get:
x insertion point
y insertion point
"PARTNO"

Block Named "FIELDPRINT" I need to get:
x insertion point
y insertion point

So maybe there is a much better way to do this that I'm not doing... or I'm heading the right direction sort of and just gotta perfect the code... I'm honestly not sure.  But maybe knowing exactly what I'm trying to do will help.

Thank-You both for being so patient with me and trying to help me understand how to do this!!

 

by: LittleOleMeDesignsPosted on 2009-04-11 at 12:44:08ID: 24123216

Ok... I've finally made some progress... to a point... When I use the code below... it works just like I want it to.. but only if I actually open the drawing in AutoCAD... so apparently something is wrong with how I'm connecting to the objectdbx... or this method just does not work with it or something...  so here's my code for the objectdbx as well as my code for getting the blocks.

Code for objectdbx method:
' ********************************************************************

 V_AutoCad = StartAutoCAD() 'Use the function located in PublicFunctions.vb to initialize AutoCAD

        Dim V_AutoCadInterfaceObject As AxDbDocument
        V_AutoCadInterfaceObject = V_AutoCad.GetInterfaceObject("ObjectDBX.AxDbDocument")

V_AutoCadInterfaceObject.Open(V_FilePathAutoCad)

' ********************************************************************
    Public Function StartAutoCAD() As Object

        On Error Resume Next

        V_AutoCad = GetObject(, "AutoCad.Application.15")
        If Err.Number <> 0 Then 'If they don't have AutoCAD open.. then open it
            Err.Clear()
            Shell("C:\Program Files\Acad2002\acad.exe", AppWinStyle.NormalFocus)
            System.Threading.Thread.Sleep(3000)
            ' Create a new session of AutoCAD using late binding
            V_AutoCad = GetObject(, "AutoCad.Application.15")
        End If
        Return V_AutoCad

    End Function

' ********************************************************************
Code for getting the block info:

            Dim strBlkName As String
            Dim strAttTag As String
            Dim objEnt As AutoCAD.AcadEntity
            Dim objBlkRef As AutoCAD.AcadBlockReference
            Dim varAtts As Object
            Dim objAtt As AutoCAD.AcadAttributeReference
            Dim I As Long

            strBlkName = "START"
            strAttTag = "FILENAME"
            For Each objEnt In V_AutoCad.ActiveDocument.ModelSpace

                If objEnt.ObjectName.ToString = "AcDbBlockReference" Then

                    objBlkRef = objEnt

                    If objBlkRef.Name = strBlkName Then
                        varAtts = objBlkRef.GetAttributes
                        For I = 0 To UBound(varAtts)
                            objAtt = (varAtts(I))
                            If objAtt.TagString = strAttTag Then
                                MsgBox(objAtt.TextString)
                            End If
                        Next I
                    End If
                End If

            Next
        Next

 

by: darrenmcwiPosted on 2009-04-14 at 21:44:00ID: 24144745

I'm on the road and don't have access to a lot of my resources, but just a guess, is that "ActiveDocument" is the issue. ActiveDocument I believe is refering to the document that's active in the editor. Can you modify it so it uses the documents collection and references the document you need by index?

 

by: LittleOleMeDesignsPosted on 2009-04-15 at 05:11:50ID: 24147057

That sounds like it could be it... I'm looking around now to see if I can find out more about how to do that... any pointers to get me looking in the right direction for how that code would go??

Thank-You again for your help!

 

by: LittleOleMeDesignsPosted on 2009-04-15 at 08:05:26ID: 24148828

OH MY GOSH... I figured it out finally!!  This works:

For Each V_AutoCadEntity In V_AutoCadInterfaceObject.ModelSpace 'For each Object in Model Space

                                              
1:

Select allOpen in new window

 

by: LittleOleMeDesignsPosted on 2009-04-15 at 08:06:24ID: 31568209

Thank-You both for all your help!!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...