Block making with VB.Net

Discussion in 'AutoCAD' started by technosterone, Mar 10, 2005.

  1. Hello, gentlemen. Is there anybody who elaborates software with VB.Net and
    can tell me how to create a block, which can be seen in a list after
    _insert command? The block making sub should get one argument.



    Public Sub MakeBlock(ByVal blkName As String)

    'blkName - the name of a block to be created



    Dim db As Database

    db =
    Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDo
    cument.Database



    ..

    ..

    ..





    End Sub





    I asked the same question couple months ago but got no answer.
     
    technosterone, Mar 10, 2005
    #1
  2. I have no .NET experience, and don't have .NET, but here's a guess. Maybe
    it'll get comments as to why it would not work.
    -- James

    Code:
    Public Sub MakeBlock(ByVal blkName As String)
    
    Dim db As Database
    Dim t As AcadDocument
    
    db =
    Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDo
    cument.Database;
    
    Dim blockObj As AcadBlock;
    Dim insertionPnt As Double[] = {0,0,0};
    
    blockObj = db.Blocks.Add(insertionPnt, blkName);
    blockObj.AddCircle insertionPnt, 1#;
    blockObj.AddCircle insertionPnt, 2#;
    
    End Sub
    
     
    James Belshan, Mar 10, 2005
    #2
  3. technosterone

    Mikko Guest

    Doesn't the supplied AutoCAD example work?

    Public WithEvents ACADApp As AutoCAD.AcadApplication

    Public Sub MakeBlock(ByVal blkName As String)
    Dim acadDoc As AutoCAD.AcadDocument
    acadDoc = ACADApp.ActiveDocument
    Dim blkColl As AutoCAD.AcadBlocks
    Dim newBlock As AutoCAD.AcadBlock
    Dim insertionPnt(2) As Double
    blkColl = acadDoc.Blocks
    insertionPnt(0) = 0.0# : insertionPnt(1) = 0.0# : insertionPnt(2) = 0.0#
    newBlock = blkColl.Add(insertionPnt, blkName)
    Dim Database As AutoCAD.AcadDatabase
    Database = acadDoc.ModelSpace.Database
    MsgBox("The number of Blocks in this database is: " & Database.Blocks.Count & vbCrLf & "Here is the block name you just created: " & Database.Blocks.Item(Database.Blocks.Count - 1).Name)
    Database = Nothing
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ACADApp = GetObject(, "AutoCAD.Application.16.1")
    Call MakeBlock(TextBox1.Text)
    End Sub
     
    Mikko, Mar 10, 2005
    #3
  4. James Belshan and Miko! Thank you.
    But as far I can see your samples may work only in VBA but not in .Net.
    Because there is a great difference between objects structure. DotNet has
    the one of ObjectARX.
    Your code will not be compiled in DotNet.
     
    technosterone, Mar 11, 2005
    #4
  5. technosterone

    Mikko Guest

    I wrote this in Visual Studio 2003. VB.NET. The code will run in a standalone exe.
     
    Mikko, Mar 11, 2005
    #5
  6. standalone exe.
    I see you point. But you used ActiveX Automation and got exe file. I use
    ObjectARX wich means compiling dll.
     
    technosterone, Mar 11, 2005
    #6
  7. His code can be compiled to a DLL and will work. ObjectARX compiles to a
    ARX file, not a DLL. VB.NET and C# compile to DLLs or EXEs. Both have the
    immenent power of ObjectARX.. Don't confuse ObjectARX with .NET. If you
    really want to use ObjectARX, you're in the wrong ng.

    What I *think* your problem is, is that you are confusing managed and
    unmanaged code. His code was unmanaged using the COM InterOp to accomplish
    the block creation. Unfortunately, any serious dev work in VB/C# will
    require you to revert to COM in most cases.

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Mar 11, 2005
    #7
  8. Here is a quick example of using the managed API to create a block in the
    active drawing.

    public ObjectId CreateBlockExample ()
    {
    Database activeDb = HostApplicationServices.WorkingDatabase;

    using (Transaction trans = activeDb.TransactionManager.StartTransaction())
    {
    //Create a new Block Table Record
    BlockTableRecord newBtr = new BlockTableRecord();
    newBtr.Name = "MyAwesomeNewBlock";
    newBtr.Origin = new Point3d(0, 0, 0);

    //Open the Block Table of the active database for writing
    BlockTable bt = (BlockTable) trans.GetObject(activeDb.BlockTableId,
    OpenMode.ForWrite);

    //Append the newBtr to the BlockTable
    bt.Add(newBtr);

    //Add it to the transaction
    activeDb.TransactionManager.AddNewlyCreatedDBObject(newBtr, true);

    trans.Commit();

    return newBtr.ObjectId;
    }
    }

    Since you're using VB you'll need to convert the "using" block to a
    "try..catch..finally" block to commit the transaction.
     
    Bobby C. Jones, Mar 11, 2005
    #8
  9. Thank you, everybody. Now I see things perfectly clear.
     
    technosterone, Mar 14, 2005
    #9
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.