See the top rated post in this thread. Click here

Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: Dot Net vs VBA ... Speed and Functionality

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

    Default Re: Dot Net vs VBA ... Speed and Functionality

    Quote Originally Posted by _gile View Post
    .... you do not need to iterate the whole database the BlockTableRecord class provides a GetBlockReferenceIds method which allows to find all references for this block. So, you'll only need to iterate the BlockTable to get all references or only some known blocks.
    Absolument!

    I completely forgot about GetBlockReferenceIds; thanks for the reminder, Gile.

    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

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

    Default Re: Dot Net vs VBA ... Speed and Functionality

    My code is composed of several "modules":
    - One that searches through folders to find the files; what I did is probably rudimentary to most people, but to me, it was a breakthrough .
    - One that reads the data from an AutoCAD file, and writes it to Excel. The "main" code is posted below (pared down to the part the actually does the searching).
    - One that reads from Excel and writes to AutoCAD (basically identical to the reading one, except AutoCAD -> Excel becomes Excel -> AutoCAD)
    - One that validates and does stuff to the Excel data (this is going to stay with the "data" Excel file, written in VBA; the reading thing will use an Excel template that already has the module in it to fill the data file).
    - A bunch of extra functions like determining if an array is allocated, something to clear the Excel file, etc ... mostly just to try and pare down the code a little for repeated stuff.
    Code:
                ' Open the drawing
                Set AcadDbx = dbxOpen(strResultArray(intFolderColumn - 1, DwgCnt), strResultArray(intFileColumn - 1, DwgCnt))
            
                ' If there are no errors and there is a file open
                If Err.Number = 0 And AcadDbx.Name <> "" Then
    
                    BlkExist = False
                    ' Determine if the requested blocks are present in the drawing
                    For Each AcadBlk In AcadDbx.Blocks
                        For BlkCnt = 0 To UBound(strBlockArray)
                            ' Check if the block matches the block we want
                            If UCase(AcadBlk.Name) = UCase(strBlockArray(BlkCnt)) Then
                                BlkExist = True
                                Exit For
                            End If
                        Next BlkCnt
                        If BlkExist Then Exit For
                    Next AcadBlk
                    ' Housekeeping
                    Set AcadBlk = Nothing
    
                    ' If the block(s) was(were) found, then proceed
                    If BlkExist Then
    
                        ' Search Modelspace
                        For Each AcadEnt In AcadDbx.ModelSpace
                            If TypeOf AcadEnt Is AcadBlockReference Then
                                Set AcadBlkR = AcadEnt
                                ' Iterate through the block array
                                For BlkCnt = 0 To UBound(strBlockArray)
                                    ' Check if the block is one we want
                                    If UCase(AcadBlkR.Name) = UCase(strBlockArray(BlkCnt)) Then
                                        If AcadBlkR.HasAttributes Then
                                            
                                            ' Store all attributes in the matrix Atts
                                            Atts = AcadBlkR.GetAttributes
                                            ' Step through each attribute in Atts
                                            For AttCnt = LBound(Atts) To UBound(Atts)
                                                ' Iterate through the array of codes
                                                Set AcadAtt = Atts(AttCnt)
                                                For CodeCnt = 0 To UBound(varAcadCodeArray(BlkCnt))
                                                    strCode = Split(varAcadCodeArray(BlkCnt)(CodeCnt), ";", -1, vbBinaryCompare)
                                                    ' Get the next attribute
                                                    If strCode(0) = AcadAtt.TagString Then
                                                        ' Write the attribute information to the result array
                                                        strResultArray(Int(strCode(1)), DwgCnt) = UCase(AcadAtt.TextString)
                                                        Exit For
                                                    End If
                                                Next CodeCnt
                                            Next AttCnt
                                            ' Housekeeping
                                            Set AcadAtt = Nothing
                                        End If
                                        ' Housekeeping
                                        Set Atts = Nothing
                                    End If
                                Next BlkCnt
                                ' Housekeeping
                                Set AcadBlkR = Nothing
                            End If
                        Next AcadEnt
                        ' Housekeeping
                        Set AcadEnt = Nothing
                    End If
                    ' Housekeeping
                    Set AcadDbx = Nothing
    The "problem" is how I did it ... I think it's not too easy for others to understand. However, you just need to imagine that strBlockArray is full of the blocks I want, then strCodeArray holds information of what attribute I need. In an ideal world, strblockarray would be 2 dimensional: strBlockArray(<blockname>,<codes>). In fact, I can't remember why I didn't do that in the first place.

    With the code I posted, there are 16 possible blocks, and they mostly hold attributed information in differing ways, and using different attribute names. So, as a result, I have a "code" system, something like "varAcadCodeArray(0) = Array("PRIME4;8", "SHEET4;16", "SEGMENT2;85")" ... where it knows that "0" is block "TITLEBLOCK", ... then PRIME4 goes into column 8 of the Excel file, SHEET4 does to column 16, and SEGMENT2 goes to 85. I used a variant array because you can't initialise 2D arrays directly in VBA (but I am pretty sure you can in basically every language with the {{a,b},{c,d}} type thing). When my program finds a block, it compares it against a list stored in strBlockArray ... if it's the block I want, it uses those codes in varAcadCodeArray to figure out where to put it in Excel.

    If I could figure out this thing, I could probably pretty easily sort out how to do it in reverse. Also, once the code does SOMETHING, I can port over all the other functionality. Also, knowing how anything is structured (i.e. what a program looks like when it ... well ... is a program) would be fantastic.

    Ideal beginning program would have a button that says "Read", it asks for an AutoCAD file (or even one hardcoded into it) to read from, and asks for a destination Excel file (again, or even hardcoded) ... then reads from block "HEAD", attribute "PRIME", and puts it into Excel cell(1,1) ... close and save Excel, close the AutoCAD "database" thing. From this basis, I could add all the functionality I need.

    nth edit: I would be more than happy to send anyone the Excel file I have that does what I need, along with a few examples that don't have a zillion entities so it'll run in maybe 2 seconds ... you'll need to have AutoCAD 2010, otherwise I am not sure it'll work, even if you edit the references.
    Last edited by e_casagrande394681; 2014-02-26 at 02:10 PM.

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

    Default Re: Dot Net vs VBA ... Speed and Functionality

    Found this C# code ... no idea how to use it:
    Code:
    [CommandMethod("CreateDatabaseFromDwgFile")]
    public static void CreateDatabaseFromDwgFile_Method()
    {
        Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
        try
        {
            using (Database db = new Database(false, true))
            {
                db.ReadDwgFile(@"c:\temp\test.dwg", FileOpenMode.OpenForReadAndAllShare, false, null);
                db.CloseInput(true);
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                    foreach (ObjectId id in btr)
                    {
                        Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                        ent.ColorIndex = 1;
                    }
    
                    tr.Commit();
                }
                db.SaveAs(@"c:\temp\test_2red.dwg", DwgVersion.Current);
            }
        }
        catch (System.Exception ex)
        {
            ed.WriteMessage(ex.ToString());
        }
    }
    It apparently opens a db, and changes all entities to red; obviously not what I need, but changing the "selection" to be blocks, then iterating through them looking for the ones I want and scooping attributed text, seems like a quick segue. Again, assuming I could make this code linked above actually DO anything.
    Last edited by e_casagrande394681; 2014-02-27 at 02:59 PM.

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

    Default Re: Dot Net vs VBA ... Speed and Functionality

    So, I can't get ANYTHING to work, so I decided to try and get "something" to work; anything .NET.

    I have yet to find a single piece of code that I can get to do anything. Inevitably it's littered with errors talking about things I have no idea about. frustrating.

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

    Default Re: Dot Net vs VBA ... Speed and Functionality

    Quote Originally Posted by e_casagrande394681 View Post
    So, I can't get ANYTHING to work, so I decided to try and get "something" to work; anything .NET.

    I have yet to find a single piece of code that I can get to do anything. Inevitably it's littered with errors talking about things I have no idea about. frustrating.
    Sorry for not being much help with this in a while; I've just recently hit my first 30 days with my new employer, so things have been hectic.

    You'll be happy with .NET API soon, but there's always growing pains... Learning the rules, remembering this keyword, or that permission, etc.



    The code above is a CommandMethod Method (or a Method with a CommandMethod attribute), which as you might guess registers an AutoCAD Command when NETLOADed into your session. Unlike LISP, .NET assemblies need only be NETLOADed once per session, and there is no such NETUNLOAD functionality (must restart session to reload an overwritten assembly).

    Since time seems to be of the essence for you, rather than spending the next year learning to develop in .NET outside of AutoCAD (Windows/Desktop, Forms, WPF Browser Applications, etc.), have you considered completing the AutoCAD 'My First Plug-in' training, DevBlog Tutorials, or perhaps even Autodesk University online courses on the topic?

    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

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

    Default Re: Dot Net vs VBA ... Speed and Functionality

    Quote Originally Posted by BlackBox View Post
    Since time seems to be of the essence for you, rather than spending the next year learning to develop in .NET outside of AutoCAD (Windows/Desktop, Forms, WPF Browser Applications, etc.), have you considered completing the AutoCAD 'My First Plug-in' training, DevBlog Tutorials, or perhaps even Autodesk University online courses on the topic?
    Funny enough, yes ... I went through the AutoCAD training (well, started anyways). I do everything EXACTLY as they say, and there are still errors. Following the "Make a project like this, name it this, put this code in there, change references to this and make sure you change this. Press this command, and enjoy."; noap.

    Doing the tutorial for VB instead of C# has less errors, but I still can't get it to do anything. Basically the first line is (after a bunch of imports and the start of the class) <CommandMethod("AdskGreeting")> _ ... "Type 'CommandMethod' is not defined" is the error I get. There are more errors too: Type 'Document' is not defined. and 'TextStyle' is not a member of 'Autodesk.AutoCAD.DatabaseService.MText'.

    It's annoying to not even be able to get past the most basic examples ... absolutely zero code I try ever works.

    Edit: As for the "works outside AutoCAD" thing, I didn't realise there would be a difference, so I'll just run it inside AutoCAD; no sweat really, since it should be able to accomplish what I need in minutes instead of hours, I don't care if the application holds AutoCAD hostage.

    Edit2: WOOHOO. I can't replicate how I got it to work, but it works! I think I clicked on some stuff that was coming up red and selected the "fix" for it; no sure what I did, but it does something!
    Last edited by e_casagrande394681; 2014-03-03 at 11:49 PM.

  7. #17
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    202
    Login to Give a bone
    0

    Default Re: Dot Net vs VBA ... Speed and Functionality

    Hi,

    I do not agree with BlackBox:
    Since time seems to be of the essence for you, rather than spending the next year learning to develop in .NET outside of AutoCAD (Windows/Desktop, Forms, WPF Browser Applications, etc.), have you considered completing the AutoCAD 'My First Plug-in' training, DevBlog Tutorials, or perhaps even Autodesk University online courses on the topic?
    According to what you say about your difficulties, you certainly need to learn the .NET basics outside of AutoCAD (Visual Studio using, OOP, and so on).
    About 'My First Plug-in', I do not think it's the best way to start becaus it wants to show the using of Overrules: one of the most complex API in the AutoCAD .NET API (which is, itself, anything but simple). You'd rather start with 'AutoCAD 201X .Net Training' on this page.
    Last edited by _gile; 2014-03-04 at 03:01 PM.

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

    Default Re: Dot Net vs VBA ... Speed and Functionality

    Quote Originally Posted by _gile View Post
    I do not agree with BlackBox:


    According to what you say about your difficulties, you certainly need to learn the .NET basics outside of AutoCAD (Visual Studio using, OOP, and so on).
    About 'My First Plug-in', I do not think it's the best way to start becaus it wants to show the using of Overrules: one of the most complex API in the AutoCAD .NET API (which is, itself, anything but simple). You'd rather start with 'AutoCAD 201X .Net Training' on this page.
    My suggestion was a 'last resort' due to unexpected schedule changes on my end, and not how I learned myself either.

    As always, I appreciate your candor, Gile.



    Quote Originally Posted by e_casagrande394681 View Post
    Doing the tutorial for VB instead of C# has less errors, but I still can't get it to do anything. Basically the first line is (after a bunch of imports and the start of the class) <CommandMethod("AdskGreeting")> _ ... "Type 'CommandMethod' is not defined" is the error I get. There are more errors too: Type 'Document' is not defined. and 'TextStyle' is not a member of 'Autodesk.AutoCAD.DatabaseService.MText'.

    It's annoying to not even be able to get past the most basic examples ... absolutely zero code I try ever works.
    It definitely sounds like you need to understand Visual Studio, and basic assembly references first. It really is not kind of Autodesk to have the assumed level of .NET API fundamentals they do, for nearly all of the training materials. Just know that you're not alone in your frustrations at this stage. There is a great deal of truth, and wisdom in Gile's comments.

    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

  9. #19
    AUGI Addict
    Join Date
    2015-12
    Posts
    2,095
    Login to Give a bone
    0

    Default Re: Dot Net vs VBA ... Speed and Functionality

    +1 to both BlackBox and _giles. Programming in dotNET isn't like LISP or VBA where you can machete a bunch of Internet code samples together and get something that works (at least on the outside). The greater degree of control demands a more rigorous and regimented approach. Once the basics are in hand, however, they can be applied to almost everything.

    Some days I'm glad I started hand-coding with DOS and an 8088 emulator.

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

    Default Re: Dot Net vs VBA ... Speed and Functionality

    I've programmed in a LOT of languages, just not "higher level" (i.e. creating full applications). I think I get the concepts, so I need to figure out the syntax. My task is quite simple, really just needing to be run from a more efficient API.

    Right now, I seem to be struggling with "non program" related problems. For example, references. Not sure why you have to use the interface to include a reference ... would be nice to be able to type something like Reference C:\something as LinkLibrary ... I know it's kindof silly, but would make code actually work when someone copies and pastes it.

    Regardless, I am making some progress, but the project I wanted this to work for is now upon me. So, I've built 5 computers from garbage I have laying around, and I am running my "old" setup on them; looks like it'll take them about 2 days each to complete their part of the task (assuming they don't crash, which is a big if), I'll combine the information, manipulate it, then do this all in reverse. Due date is fast approaching.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. MAP Functionality
    By mjfarrell in forum Civil 3D Wish List
    Replies: 0
    Last Post: 2011-11-23, 06:57 AM
  2. Games' speed vs Revit Speed
    By rudolfesterhuyse in forum Revit Architecture - General
    Replies: 25
    Last Post: 2010-04-09, 02:23 PM
  3. API speed
    By robert.ronnholm in forum Robot Structural Analysis
    Replies: 2
    Last Post: 2009-12-18, 01:04 PM
  4. P4 HT & speed?
    By truevis in forum Revit - Rendering
    Replies: 0
    Last Post: 2006-09-20, 08:02 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
  •