Originally Posted by
artisteroi
heres what I got from Kean
Originally Posted by
artisteroi
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'~