- public void Output(string xmbh, string fw, string szc, ObjectId[] Ids, ObjectId[] AllIds)
- {
- var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- var ed = doc.Editor;
- var db = doc.Database;
- using (doc.LockDocument())
- {
- //新建图层,复制打印项,改图层打印属性
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable blt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- ObjectIdCollection objectIds = new ObjectIdCollection();
- foreach (var p in Ids)
- {
- var a = tr.GetObject(p, OpenMode.ForWrite);
- if (a is Polyline pl)
- {
- pl.UpgradeOpen();
- pl.ConstantWidth = 0.05;
- pl.DowngradeOpen();
- }
- DBObject b = (DBObject)a.Clone();
- objectIds.Add(btr.AppendEntity((Entity)b));
- tr.AddNewlyCreatedDBObject(b, true);
- }
- LayerTable table = tr.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
- if (!table.Has("打印层"))
- {
- LayerTableRecord record = new LayerTableRecord();
- record.Name = "打印层";
- ObjectId tbid = table.Add(record);
- tr.AddNewlyCreatedDBObject(record, true);
- Group grp = new Group();
- grp.Append(objectIds);
- grp.SetLayer(tbid);
- }
-
- foreach (ObjectId id in table)
- {
- LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(id, OpenMode.ForWrite);
- if (ltr.Name != "打印层")
- {
- ltr.IsPlottable = false;
- }
- }
- tr.Commit();
- }
- var ext = Ids.GetExtents(SCTPWYJ);
- string outputFolder = Path.Combine("C:\\WorkFile", xmbh);
- Directory.CreateDirectory(outputFolder);
- // 输出JPG
- string outputFile = Path.Combine(outputFolder, $"层图_{fw}_{szc}.jpg");
- _plot.Plot(ext.MinPoint, ext.MaxPoint, outputFile);
- // 输出DWG
- string outputFile2 = Path.Combine(outputFolder, $"层图_{fw}_{szc}.dwg");
- _dos.SaveAs(Ids, outputFile2);
- //还原
- using (var tr = db.TransactionManager.StartTransaction())
- {
- BlockTable blt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- foreach (ObjectId entId in btr)
- {
- Entity entity = tr.GetObject(entId, OpenMode.ForRead) as Entity;
- if (entity.Layer == "打印层")
- {
- entity.UpgradeOpen();
- try
- {
- entity.Erase();
- }
- catch
- {
- continue;
- }
- }
- }
- LayerTable table = tr.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
- foreach (ObjectId id in table)
- {
- LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(id, OpenMode.ForWrite);
- if (ltr.Name == "打印层")
- {
- ltr.Erase(true);
- continue;
- }
- ltr.IsPlottable = true;
- }
- tr.Commit();
- }
- }
- }
|