private void SaveAsDxf(string source, string dest, string drawingName) { try { if (drawingName == null) { throw new ArgumentNullException(nameof(drawingName)); } _logger.AddLog(String.Format("Drawing to Open: {0}", source)); Autodesk.AutoCAD.DatabaseServices.Database oldDb = HostApplicationServices.WorkingDatabase; using (Autodesk.AutoCAD.DatabaseServices.Database db = new Autodesk.AutoCAD.DatabaseServices.Database(false, true)) { db.ReadDwgFile(source, FileOpenMode.OpenForReadAndWriteNoShare, true, null); db.CloseInput(true); HostApplicationServices.WorkingDatabase = db; var dxfout = Path.Combine(dest, drawingName.Replace(".DWG", ".DXF")); HostApplicationServices.WorkingDatabase = oldDb; db.DxfOut(dxfout, 16, DwgVersion.Current); } } catch (Exception ex) { _logger.LogException(ex); } }
/// <summary> /// Экспорт блока в файл - файл в корне текущего чертежа с именем блока. /// Точка вставки блока - 0,0 /// </summary> public void ExportToFile() { using (var db = new Autodesk.AutoCAD.DatabaseServices.Database(true, true)) { db.CloseInput(true); var ids = new ObjectIdCollection(new[] { IdBlRef }); var idMS = SymbolUtilityServices.GetBlockModelSpaceId(db); using (IdMapping map = new IdMapping()) { db.WblockCloneObjects(ids, idMS, map, DuplicateRecordCloning.Replace, false); // перенос блока в ноль var idBlRefMap = map[IdBlRef].Value; if (!idBlRefMap.IsNull) { using (var t = db.TransactionManager.StartTransaction()) { var blRef = idBlRefMap.GetObject(OpenMode.ForWrite, false, true) as BlockReference; blRef.Position = Point3d.Origin; // Изменение вида if (blRef.Bounds.HasValue) { try { zoomDb(db, blRef.Bounds.Value); // Перенос штриховки на задний план var btrApart = blRef.BlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord; var orders = btrApart.DrawOrderTableId.GetObject(OpenMode.ForWrite) as DrawOrderTable; var idsHatch = new ObjectIdCollection(); foreach (var idEnt in btrApart) { if (idEnt.ObjectClass == RXClass.GetClass(typeof(Hatch))) { idsHatch.Add(idEnt); } } if (idsHatch.Count > 0) { orders.MoveToBottom(idsHatch); } // Превью чертежа из блока квартиры db.ThumbnailBitmap = new Bitmap(btrApart.PreviewIcon, new Size(320, 270)); } catch { } } t.Commit(); } db.SaveAs(File, DwgVersion.Current); } } //Inspector.AddError($"Экспортирован блок {Name} в файл {File}", IdBlRef, icon: System.Drawing.SystemIcons.Information); } }
private void DeletePoly2D3DObjects(string source, string dest, string drawingName) { bool eraseOrig = true; try { if (drawingName == null) { throw new ArgumentNullException(nameof(drawingName)); } _logger.AddLog(String.Format("Drawing to Open: {0}", source)); Autodesk.AutoCAD.DatabaseServices.Database oldDb = HostApplicationServices.WorkingDatabase; using (Autodesk.AutoCAD.DatabaseServices.Database db = new Autodesk.AutoCAD.DatabaseServices.Database(false, true)) { db.ReadDwgFile(source, FileOpenMode.OpenForReadAndWriteNoShare, true, null); db.CloseInput(true); HostApplicationServices.WorkingDatabase = db; using (Transaction tr = db.TransactionManager.StartTransaction()) { // Collect our exploded objects in a single collection var selected = BBC.Common.AutoCAD.AcadUtilities. GetAllObjectIdsInModel(db, false); DBObjectCollection objs = new DBObjectCollection(); // Loop through the selected objects foreach (ObjectId so in selected) { // Open one at a time Entity ent = (Entity)tr.GetObject( so, OpenMode.ForRead ); ; if (ent.GetType() == typeof(Polyline3d) || ent.GetType() == typeof(Polyline2d) || ent.GetType() == typeof(Acad.Polyline) || ent.GetType() == typeof(Line)) { if (eraseOrig) { ent.UpgradeOpen(); ent.Erase(); } } } tr.Commit(); } var output = Path.Combine(_sdir, "S-" + drawingName + ".DWG"); var dxfout = output.Replace(".DWG", ".DXF"); db.SaveAs(output, DwgVersion.Current); HostApplicationServices.WorkingDatabase = oldDb; db.DxfOut(dxfout, 16, DwgVersion.Current); } } catch (Exception ex) { _logger.LogException(ex); } }