Hello,
I also having Problems adding blocks in layout. i have scaled the block and want add it in paper space.
the code below gives you the block reference of selected entities.
/////////////////////////////////////////////////////////////
public static BlockReference JoinEntitiesandScale(Point3d Point,double scale)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptSelectionOptions opts = new PromptSelectionOptions();
opts.MessageForAdding = "Select entities: ";
PromptSelectionResult res = ed.GetSelection(opts);
SelectionSet selSet = res.Value;
ObjectId[] idArray = selSet.GetObjectIds();
BlockReference insert;
//ObjectId blockId = ObjectId.Null;
Transaction tr = db.TransactionManager.StartTransaction();
try
{
using (DocumentLock locDoc = ed.Document.LockDocument())
{
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
//bt.UpgradeOpen();
BlockTableRecord record = new BlockTableRecord();
record.Name = "123";
bt.UpgradeOpen();
if (!bt.Has(record.Name))
{
ObjectId btrId = bt.Add(record);
tr.AddNewlyCreatedDBObject(record, true);
}
else
{
record = (BlockTableRecord)tr.GetObject(bt[record.Name], OpenMode.ForWrite);
}
ObjectIdCollection collection = new ObjectIdCollection(idArray);
IdMapping mapping = new IdMapping();
db.DeepCloneObjects(collection, record.ObjectId, mapping, false);
Matrix3d newscaleMatrix = Matrix3d.Scaling(scale, Point);
//BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead);
foreach (ObjectId entId in record)
{
DBObject obj = tr.GetObject(entId, OpenMode.ForRead);
Entity ent = obj as Entity;
if (ent != null)
{
ent.UpgradeOpen();
ent.TransformBy(newscaleMatrix);
ent.DowngradeOpen();
}
}
BlockTableRecord curSpace = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
insert = new BlockReference(Point, record.ObjectId);
curSpace.AppendEntity(insert);
tr.AddNewlyCreatedDBObject(insert, true);
tr.Commit();
}
}
if (insert != null)
{
return insert;
}
}
catch (System.Exception)
{
}
return null;
}
/////////////////////////////////////////////////////////////////////
after creating a block reference i want to add it in layout. my Code is.
////////////////////////////////////////////////////////////////
public static void Layout()
{
var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
if (doc == null)
return;
var db = doc.Database;
var ed = doc.Editor;
try
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
if (pr.Status == PromptStatus.OK)
{
DCS.RxServices.CADUtility.JoinEntitiesandScale(new Point3d(pr.Value.X, pr.Value.Y, 0), 0.25);
ObjectId id = LayoutManager.Current.CreateAndMakeLayoutCurrent("Plot", true);
Layout lay = (Layout)tr.GetObject(id, OpenMode.ForWrite);
lay.SetPlotSettings(
//"ISO_full_bleed_2A0_(1189.00_x_1682.00_MM)", // Try this big boy!
//"ANSI_B_(11.00_x_17.00_Inches)",
"ISO A4 (210.00 x 297.00 MM)",
"monochrome.ctb",
"DWGF6 ePlot.pc3"
);
}
tr.commit();
}
PromptSelectionOptions opts = new PromptSelectionOptions();
opts.MessageForAdding = "Select entities: ";
PromptSelectionResult res = ed.GetSelection(opts);
//1 block selected
if (res.Status == PromptStatus.OK)
{
SelectionSet selSet = res.Value;
ObjectId[] idArray = selSet.GetObjectIds();
Transaction tr1 = db.TransactionManager.StartTransaction();
using (tr1)
{
BlockTableRecord space = tr1.GetObject(SymbolUtilityServices.GetBlockPaperSpaceId(db), OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId entId in idArray)
{
Entity entity = tr1.GetObject(entId, OpenMode.ForWrite) as Entity;
Entity entClone = entity.Clone() as Entity;
space.AppendEntity(entity);
tr1.AddNewlyCreatedDBObject(entity, true);
ObjectIdCollection collection = new ObjectIdCollection(idArray);
IdMapping mapping = new IdMapping();
db.DeepCloneObjects(collection, space.ObjectId, mapping, false);
entity.Erase();
}
tr1.Commit();
}
ed.Regen();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
}
}
///////////////////////////////////////////////////////
i want to add atleast 4 block in a layout by scaling it down. i have tried this using view ports and have created 4 viewports in a layout but is there any other way i can add blocks without using viewports.
Regards.