See the top rated post in this thread. Click here

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

Thread: Create a block from inserted objects

  1. #1
    I could stop if I wanted to artisteroi's Avatar
    Join Date
    2007-02
    Location
    Tampa, FL
    Posts
    271
    Login to Give a bone
    0

    Default Create a block from inserted objects

    Hi Everybody!

    Today I am trying to create a block from multiple other blocks.
    I have a routine that inserts a block and then scales it and repeates this action several times. So when it is done I have a cluster of 2 to 20 blocks all with the same name but at different scales and positions. now I want to select all the peices and make a new block out of the set with a new name.

    My thinking was that I could write each peice to a named selection set as I am inserting them and then at the end take all the items in the selection set and convert it to a block. Does that sound like it will work? And can someone show me how to do it because I am WAY over my head.

    Any help would be appreciated

  2. #2
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    1

    Default Re: Create a block from inserted objects

    Quote Originally Posted by artisteroi View Post
    Hi Everybody!

    Today I am trying to create a block from multiple other blocks.
    I have a routine that inserts a block and then scales it and repeates this action several times. So when it is done I have a cluster of 2 to 20 blocks all with the same name but at different scales and positions. now I want to select all the peices and make a new block out of the set with a new name.

    My thinking was that I could write each peice to a named selection set as I am inserting them and then at the end take all the items in the selection set and convert it to a block. Does that sound like it will work? And can someone show me how to do it because I am WAY over my head.

    Any help would be appreciated
    Take a look at this page:

    http://through-the-interface.typepad...using-net.html

    ~'J'~

  3. #3
    I could stop if I wanted to artisteroi's Avatar
    Join Date
    2007-02
    Location
    Tampa, FL
    Posts
    271
    Login to Give a bone
    0

    Default Re: Create a block from inserted objects

    Hi Fixo,

    That works pretty good for one insertion at a time, but I can't seem to get it to select more than the one block.

    any hints on how to get it to add the last 4 items to the block?

  4. #4
    I could stop if I wanted to artisteroi's Avatar
    Join Date
    2007-02
    Location
    Tampa, FL
    Posts
    271
    Login to Give a bone
    0

    Default Re: Create a block from inserted objects

    heres what I got from Kean
    Code:
     
            // Add some lines to the block to form a square
            // (the entities belong directly to the block)
     
            DBObjectCollection ents = SquareOfLines(5);
            foreach (Entity ent in ents)
            {
              btr.AppendEntity(ent);
              tr.AddNewlyCreatedDBObject(ent, true);
            }
    which works ok but is incomplete for my purposes. Instead of putting in the SquareOfLines, I need to put in several other already existing blocks that are already in model space.

  5. #5
    All AUGI, all the time
    Join Date
    2003-10
    Posts
    706
    Login to Give a bone
    0

    Default Re: Create a block from inserted objects

    Couldn't you record the handle of each newly created block reference once it's inserted and then create a selection set of all the collected handles and then pass it to the new block collection?

  6. #6
    I could stop if I wanted to artisteroi's Avatar
    Join Date
    2007-02
    Location
    Tampa, FL
    Posts
    271
    Login to Give a bone
    0

    Default Re: Create a block from inserted objects

    will that work? I dont know, I am way out of my league here. I tried it with the block name but the system wont take that since it is the same name over and over. "already exists" error

    Can you show me how to get the handle info? I've never done that before. 20 year acad vet and code still makes me feel like such a newb

  7. #7
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Create a block from inserted objects

    Quote Originally Posted by artisteroi View Post
    heres what I got from Kean
    Quote Originally Posted by artisteroi View Post
    Code:
           // Add some lines to the block to form a square
           // (the entities belong directly to the block)
     
           DBObjectCollection ents = SquareOfLines(5);
           foreach (Entity ent in ents)
           {
             btr.AppendEntity(ent);
             tr.AddNewlyCreatedDBObject(ent, true);
           }
    which works ok but is incomplete for my purposes. Instead of putting in the SquareOfLines, I need to put in several other already existing blocks that are already in model space.
    Here is one I wrote for you, fully based on Kean Walmsley's code

    Code:
    public static void CreateBlockFromSelection()
    {
    Document doc =
    AcadApp.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;
    try
    {
    Transaction tr =
    db.TransactionManager.StartTransaction();
    using (tr)
    {
    // Get the block table from the drawing
    BlockTable bt =
    (BlockTable)tr.GetObject(
    db.BlockTableId,
    OpenMode.ForRead
    );
    // Check the block name, to see whether it's
    // already in use
    PromptStringOptions pso =
    new PromptStringOptions(
    "\nEnter new block name: "
    );
    pso.AllowSpaces = true;
    // A variable for the block's name
    string blkName = "";
    do
    {
    PromptResult pr = ed.GetString(pso);
    // Just return if the user cancelled
    // (will abort the transaction as we drop out of the using
    // statement's scope)
    if (pr.Status != PromptStatus.OK)
    return;
    try
    {
    // Validate the provided symbol table name
    SymbolUtilityServices.ValidateSymbolName(
    pr.StringResult,
    false
    );
    // Only set the block name if it isn't in use
    if (bt.Has(pr.StringResult))
    ed.WriteMessage(
    "\nA block with this name already exists."
    );
    else
    blkName = pr.StringResult;
    }
    catch
    {
    // An exception has been thrown, indicating the
    // name is invalid
    ed.WriteMessage(
    "\nInvalid block name."
    );
    }
    } while (blkName == "");
    // Create our new block table record...
    BlockTableRecord btr = new BlockTableRecord();
    // ... and set its properties
    btr.Name = blkName;
    // Add the new block to the block table
    bt.UpgradeOpen();
    ObjectId btrId = bt.Add(btr);
    tr.AddNewlyCreatedDBObject(btr, true);
    // Add some lines to the block to form a 
    //selection on screen
    // (the entities belong directly to the block)
    // Use an options object to specify how the
    // selection occurs (in terms of prompts)
    PromptSelectionOptions ppso =
    new PromptSelectionOptions();
    ppso.MessageForAdding = "\nSelect objects to add to block: ";
    // Perform our selection
    PromptSelectionResult psr = ed.GetSelection(ppso);
    if (psr.Status != PromptStatus.OK)
    return;
    // Assuming something was selected...
    if (psr.Value.Count == 0)
    return;
    PromptPointOptions ppo = new PromptPointOptions("\nPick origin oint for block: ");
    PromptPointResult pres = ed.GetPoint(ppo);
    if (pres.Status != PromptStatus.OK)
    return;
    ObjectIdCollection cids = new ObjectIdCollection();
    // add selected objects to objectid collection
    foreach (SelectedObject so in psr.Value)
    {
    DBObject ent = (DBObject)so.ObjectId.GetObject(OpenMode.ForRead);
    cids.Add(ent.ObjectId);
    }
    btr.AssumeOwnershipOf(cids);
    btr.Origin = pres.Value;
    btr.DowngradeOpen();
    // Add a block reference to the model space
    BlockTableRecord ms =
    (BlockTableRecord)tr.GetObject(
    bt[BlockTableRecord.ModelSpace],
    OpenMode.ForWrite
    );
    // insert in the same location 
    BlockReference br =
    new BlockReference(pres.Value, btrId);
    ms.AppendEntity(br);
    tr.AddNewlyCreatedDBObject(br, true);
    // Commit the transaction
    tr.Commit();
    // Report what we've done
    ed.WriteMessage(
    "\nCreated block named \"{0}\" containing {1} entities.",
    blkName, psr.Value.Count
    );
    }
    }
    catch (Autodesk.AutoCAD.Runtime.Exception ex)
    {
    ed.WriteMessage("{0}", ex.StackTrace);
    }
    }
    ~'J'~

  8. #8
    I could stop if I wanted to artisteroi's Avatar
    Join Date
    2007-02
    Location
    Tampa, FL
    Posts
    271
    Login to Give a bone
    0

    Default Re: Create a block from inserted objects

    Quote Originally Posted by tony.tanzillo View Post
    You haven't posted any code so it isn't clear how you're inserting
    the blocks.

    You might get better help by making things a bit clearer
    Sorry Tony. My boss won't let me post any code. I almost got fired the last time I did that. The best I can do is try to explain it and hope I make myself clear. I wouldn't blame you if you didn't want to help me.

  9. #9
    I could stop if I wanted to artisteroi's Avatar
    Join Date
    2007-02
    Location
    Tampa, FL
    Posts
    271
    Login to Give a bone
    0

    Default Re: Create a block from inserted objects

    Fixo,
    Your code works pretty good. Thanks for all the help. It was very usefull to us.

    We are gonna automate the prompt part so that the program will just select the parts by itself and then generate the block name automatically also. I think we can probably handle that part because we have done it before.

    You've done us a big favor. Thanks

  10. #10
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Create a block from inserted objects

    Quote Originally Posted by artisteroi View Post
    Fixo,
    Your code works pretty good. Thanks for all the help. It was very usefull to us.

    We are gonna automate the prompt part so that the program will just select the parts by itself and then generate the block name automatically also. I think we can probably handle that part because we have done it before.

    You've done us a big favor. Thanks
    You're quite welcome
    Thanks to Kean Walmsley

    ~'J'~

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 2012-07-04, 07:40 AM
  2. DB that I create, is not the DB that gets inserted!!
    By bmonk in forum Dynamic Blocks - Technical
    Replies: 9
    Last Post: 2006-10-27, 02:49 PM
  3. After I create a Block via -Block it Erases the Objects
    By ReachAndre in forum AutoCAD General
    Replies: 17
    Last Post: 2006-06-07, 12:03 PM
  4. Existing Block does not update when new Block is inserted
    By Rachel Ritchie in forum AutoCAD General
    Replies: 4
    Last Post: 2005-12-05, 03:04 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
  •