Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: How To Find Blocks (Logic)

  1. #1
    Active Member
    Join Date
    2013-06
    Posts
    72
    Login to Give a bone
    0

    Default How To Find Blocks (Logic)

    I am going to preface this with the fact I am VERY new to .NET programming.

    I am trying to migrate a tool I created from VBA to VB.NET, and have run into a problem that I am surprisingly unable to figure out with Google.

    What my tool does it collect information from a litany of attributed blocks and outputs this information, in a very specific format, into an Excel spreadsheet. From there I can edit a bunch of things, then it all goes back into the blocks. In VBA this is INSANELY slow, as it has to check every single entity to see if it is a block, but in .NET, I can start out by searching JUST for blocks, and this should speed things up a lot.

    My problem: How do I read attributes from a named block (lets say it is ALWAYS called "TEST_BLOCK").

    I found some sample code, but it unfortunately gets a bit cloudy because it CREATES a block then edits it, so I am having a problem establishing something that I can use the .attributecollection "thing" on.

    Here's what I tried.
    From a BlockTable type variable, I can get an ObjectID using the block name
    With the ObjectID, I can get a BlockTableRecord

    I am trying to get from BlockTableRecord to BlockReference, because BlockReference is where I can get .AttributeCollection, and then I can get the .ObjectID, .DBObject, .AttributeReference, then .Tag and .TextString

    "important" code looks something like this (note: commented line is illegal):
    Code:
                        ' define the variable that holds all the blocks
                        Dim acBlkTbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
    
                        ' define the "record ID to process" variable (default: null)
                        Dim blkRecId As ObjectId = ObjectId.Null
    
                        ' if block exists, define it as the "record ID to process"
                        If acBlkTbl.Has("TEST_BLOCK") Then
                            blkRecId = acBlkTbl("TEST_BLOCK")
                        End If
    
                        ' if we found our block/record ID, then we need to process it
                        If blkRecId <> ObjectId.Null Then
    
                            ' define a variable to hold the block's table record
                            Dim acBlkTblRec As BlockTableRecord
                            acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead)
    
                            ' if the block's record has attributes (will account for "illegal" blocks) ...
                            If acBlkTblRec.HasAttributeDefinitions Then
    
                                ' This line below is illegal :(
                                Dim acBlkRef As BlockReference = acTrans.GetObject(acBlkTblRec.Id, OpenMode.ForRead)
    
                                Dim attCol As AttributeCollection = acBlkRef.AttributeCollection
    
                                For Each objID As ObjectId In attCol
    
                                    Dim dbObj As DBObject = acTrans.GetObject(objID, OpenMode.ForRead)
    
                                    Dim acAttRef As AttributeReference = dbObj
    
                                    MsgBox("Tag " & acAttRef.Tag & vbCrLf & "Value: " & acAttRef.TextString & vbCrLf)
    
                                Next
    
                            Else
                                MsgBox("Drawing has an illegal 'TEST_BLOCK' block")
                            End If
                        Else
                            MsgBox("No block with name TEST_BLOCK found")
                        End If
    It seems I don't quite get the "logic" of how you find blocks, and the way that my old code worked is too different for me to figure out how to segue, given my current knowledge.

    In VBA, it goes AcadEntity->BlockReference ... but many of my drawings have 1 million+ entities, so this takes a very long time.
    Last edited by e_casagrande394681; 2015-01-15 at 05:52 PM.

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: How To Find Blocks (Logic)

    Consider a foreach loop, iterating the ObjectIdCollection returned from a call to <BlockTableRecord>.GetBlockReferenceIds() Method.



    .NET Developer Rule #1569743540 - Object Browser, and Intellisense are your friends.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  3. #3
    Active Member
    Join Date
    2012-11
    Location
    Italy
    Posts
    65
    Login to Give a bone
    0

    Default Re: How To Find Blocks (Logic)

    I'm trying NET too. But still care for VBA I wonder why you don't use selectionsets to isolate referenceblocks and the loop through them.

  4. #4
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: How To Find Blocks (Logic)

    Quote Originally Posted by RICVBA View Post
    I'm trying NET too. But still care for VBA I wonder why you don't use selectionsets to isolate referenceblocks and the loop through them.
    This presents a great opportunity to test one of the many differences between APIs, if you're game?



    Code both in .NET API... The first obtaining a SelectionSet using a Filter for "TEST_BLOCK", and iterating as you've described... The second instead obtaining the BlockTableRecord for "TEST_BLOCK", and iterating <BlockTableRecord>.GetBlockReferenceIds().

    Consider both the amount of code needed for each, as well as the expediency, capability\portability (does it work on MdiActiveDocument only? Or does it work generically, on side-Databases as well?), etc.



    Very interested to see your findings.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  5. #5
    Active Member
    Join Date
    2012-11
    Location
    Italy
    Posts
    65
    Login to Give a bone
    0

    Default Re: How To Find Blocks (Logic)

    Hi BB
    Well I'd do that but my NET practicality as for now is limited to babbling some code I find here and there and trying adapting it to my need. So, no idea (yet) of what terms like "side database", "blocktable record" and "MdiActiveDocument" even mean... And, finally, I'm still hanging on the debugging problem you're already aware of.
    But that said I'll check this subject out as soon as I'll get there.
    From your words I argue that selectionsets would be the trickiest and less portable way.

    On the other hand I'm curious about Casagrande's possibility of reusing his VBA code adding selectionset features rather than iterating the whole database.

  6. #6
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: How To Find Blocks (Logic)

    Quote Originally Posted by RICVBA View Post
    Hi BB
    Well I'd do that but my NET practicality as for now is limited to babbling some code I find here and there and trying adapting it to my need. So, no idea (yet) of what terms like "side database", "blocktable record" and "MdiActiveDocument" even mean... And, finally, I'm still hanging on the debugging problem you're already aware of.
    But that said I'll check this subject out as soon as I'll get there.
    From your words I argue that selectionsets would be the trickiest and less portable way.

    On the other hand I'm curious about Casagrande's possibility of reusing his VBA code adding selectionset features rather than iterating the whole database.
    The best part, is that you think your approach is any different than my own! Ehhehehe

    In no particular order....

    The only way I know of porting VBA to VB.NET, is via the 'magic macro'. I first started with VB.NET myself, and then I saw all of the additional code samples, and others willing to help, when coding in C# - haven't looked back at VB since adopting C#.

    .NET MdiActiveDocument is somewhat equivalent to VBA ThisDrawing, only as of AutoCAD a 2015, can be Null due to the advent of the Start tab, and other non-Drawing Document Windows.

    Side-Database refers to opening a Drawing's Database via ReadDwgFile() Method, without opening the Document in the Editor; this is similar, and more capable than ObjectDBX.

    All DBObjects in a Drawing's (aka Document's) Database, are children of a parent BlockTable, such as ModelSpace, or PaperSpace (generally speaking). A BlockReference's definition is a BlockTableRecord which belongs to the BlockTable.

    .NET API is often exponentially faster than ActiveX APIs, so iterating the Database is not uncommon, as it also lends to make a given Method more generic, and applicable to both MdiActiveDocument.Database, HostApplicationServices.WorkingDatabase, and that which can be opened via ReadDwgFile().


    HTH



    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  7. #7
    Active Member
    Join Date
    2012-11
    Location
    Italy
    Posts
    65
    Login to Give a bone
    0

    Default Re: How To Find Blocks (Logic)

    Quote Originally Posted by BlackBox View Post
    The best part, is that you think your approach is any different than my own! Ehhehehe

    In no particular order....

    The only way I know of porting VBA to VB.NET, is via the 'magic macro'. I first started with VB.NET myself, and then I saw all of the additional code samples, and others willing to help, when coding in C# - haven't looked back at VB since adopting C#.

    .NET MdiActiveDocument is somewhat equivalent to VBA ThisDrawing, only as of AutoCAD a 2015, can be Null due to the advent of the Start tab, and other non-Drawing Document Windows.

    Side-Database refers to opening a Drawing's Database via ReadDwgFile() Method, without opening the Document in the Editor; this is similar, and more capable than ObjectDBX.

    All DBObjects in a Drawing's (aka Document's) Database, are children of a parent BlockTable, such as ModelSpace, or PaperSpace (generally speaking). A BlockReference's definition is a BlockTableRecord which belongs to the BlockTable.

    .NET API is often exponentially faster than ActiveX APIs, so iterating the Database is not uncommon, as it also lends to make a given Method more generic, and applicable to both MdiActiveDocument.Database, HostApplicationServices.WorkingDatabase, and that which can be opened via ReadDwgFile().


    HTH



    Cheers
    Thank you BB.
    To me you more and more get looking like Darth Vader trying to draw people towards the dark side of the Force. Where the Force is AUTOCAD automation and its dark side is NET, being VBA its (first) proper side! And I admit you ' re really smart at it and effective, too! They (Microsoft and Autodesk) should hire you for their NET conquering strategy. Should I ever succeed in "getting there" it'll be mostly (if not only) per your contribution. (and that will be forever blamed on you!)

  8. #8
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: How To Find Blocks (Logic)

    Quote Originally Posted by RICVBA View Post
    Thank you BB.
    To me you more and more get looking like Darth Vader trying to draw people towards the dark side of the Force. Where the Force is AUTOCAD automation and its dark side is NET, being VBA its (first) proper side! And I admit you ' re really smart at it and effective, too! They (Microsoft and Autodesk) should hire you for their NET conquering strategy. Should I ever succeed in "getting there" it'll be mostly (if not only) per your contribution. (and that will be forever blamed on you!)
    That is incredibly kind of you to say, RICVBA; I'm happy to help.



    The funny thing is, I stumbled upon development because I was bored of the mind-numbing repetition of my work as an entry level CAD Tech after re-entering civilian life. I then found out that CAD forums even existed, and started to see how popular something called 'LISP' was, but had no idea what it was (late 2009). I never would have guessed I would even understand development concepts, let alone be any good at actually developing, it was just a slippery slope of 'huh, that was easy, and it let me do in 10 seconds what used to take me a minute or so... Wonder what else I can do?'

    The curiosity alone was enough to hook me into finding limitations, which introduced even more questions about how LISP functions work, how to create new ones to expand LISP API, etc... Not having an answer to 'why?' was a driving force for me. I've always been fascinated by solving problems, complex puzzles, and the like. AUGI Wish List items are often out of my reach (I simply cannot modify AutoCAD's core), but every once in a while one crops up that I can solve using code, which brings me a great deal of joy.

    In full disclosure, I'm still very much a student of development, and feel that I'm just scratching the surface of what I can do... Hopefully I live long enough to find out?! Hence the "Potential has a shelf life" quote in my signature. Much like Detective Officer Nicholas Angel, I find it quite difficult to 'switch off', thankfully I have the captivating Mrs. BlackBox to pull me back when needed.



    I'd love to work for Microsoft, or Autodesk one day - was offered a recommendation to interview for Autodesk's Frontline team (training, technical support wing), but there was too much travel needed - perhaps I'll be fortunate enough to earn a spot on the AutoCAD or Civil 3D development teams one day? *not sure*



    As for .NET, if it is the Dark Side, then I'm happy to be there... Although, I have always been one with a particularly dark sense of humor (recent example). *chortle*

    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  9. #9
    Active Member
    Join Date
    2013-06
    Posts
    72
    Login to Give a bone
    0

    Default Re: How To Find Blocks (Logic)

    Quote Originally Posted by BlackBox View Post
    This presents a great opportunity to test one of the many differences between APIs, if you're game?

    Code both in .NET API... The first obtaining a SelectionSet using a Filter for "TEST_BLOCK", and iterating as you've described... The second instead obtaining the BlockTableRecord for "TEST_BLOCK", and iterating <BlockTableRecord>.GetBlockReferenceIds().

    Consider both the amount of code needed for each, as well as the expediency, capability\portability (does it work on MdiActiveDocument only? Or does it work generically, on side-Databases as well?), etc.
    Quote Originally Posted by BlackBox View Post
    Consider a foreach loop, iterating the ObjectIdCollection returned from a call to <BlockTableRecord>.GetBlockReferenceIds() Method.

    .NET Developer Rule #1569743540 - Object Browser, and Intellisense are your friends.
    Quote Originally Posted by RICVBA View Post
    I'm trying NET too. But still care for VBA I wonder why you don't use selectionsets to isolate referenceblocks and the loop through them.
    I love when the forum says that some token has expired and deletes my enormous message.

    Anyhow, from the above quotes, I have no idea how to use SelectionSets in VBA; my code is created in Excel, and I am pretty sure you can't create selection sets. I tried to figure this out some time ago, but I found ZERO examples on the Internet back then, and a quick search now is showing the exact same results.

    As for using the foreach, that's probably the solution for this problem in .NET; thank you. Seems like I need to figure out what an Object Browser is, as it's apparently pretty useful.

    As for speed testing, I will definitely do that once I have working .NET code. I can easily do the VBA "side loaded" way through all entities, though no idea about selectionsets. Once I have the .NET code, I can test that, and I think making selection sets in .NET is pretty easy, and can be done for side loaded documents, so with relatively little editing.

    If I can get a simple example working with all methods, I would happily compare them. However, I think I need to re-write my VBA example to run from AutoCAD to work.
    Last edited by e_casagrande394681; 2015-01-16 at 07:24 PM.

  10. #10
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: How To Find Blocks (Logic)

    As I understand it, making selections is a function of the Editor, and if your Document is opened as a Side-Databse using ReadDwgFile(), methinks these are mutually exclusive (meaning you either iterate the entire BlockTable, or query the specific BlockTableRecord's 'reference Ids'.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

Page 1 of 3 123 LastLast

Similar Threads

  1. Anyone know where I can find Blocks of dental chairs?
    By dwe61 in forum AutoCAD General
    Replies: 7
    Last Post: 2012-09-27, 10:18 AM
  2. Where can I find free CAD blocks?
    By gbelous in forum AutoCAD General
    Replies: 4
    Last Post: 2010-01-28, 05:54 PM
  3. Find Replace Text in Blocks
    By shadow_kris in forum AutoLISP
    Replies: 3
    Last Post: 2008-01-29, 08:20 PM
  4. Can't find Dynamic Blocks
    By Darren Allen in forum Dynamic Blocks - Technical
    Replies: 4
    Last Post: 2007-12-05, 03:01 PM
  5. How to find the exploded blocks
    By vkotesh in forum AutoLISP
    Replies: 5
    Last Post: 2006-01-06, 02:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •