public static System.Drawing.Image GetBlockImageFromDrawing(string blockName) { System.Drawing.Bitmap blockBmp = null; Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database; Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; ObjectId BlockId = ObjectId.Null; using (Transaction nativeTrans = db.TransactionManager.StartTransaction()) { using (BlockTable bt = nativeTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable) { if (bt.Has(blockName)) { BlockId = bt[blockName]; } } } Manager gsm = doc.GraphicsManager; // now set the block sizes to something standard Point2d screenSize = (Point2d)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("SCREENSIZE"); // set up the image sizes int width = (int)screenSize.X; int height = (int)screenSize.Y; // now create an off screen device KernelDescriptor descriptor = new KernelDescriptor(); descriptor.addRequirement(Autodesk.AutoCAD.UniqueString.Intern("3D Drawing")); GraphicsKernel kernal = Manager.AcquireGraphicsKernel(descriptor); using (Device offDevice = gsm.CreateAutoCADOffScreenDevice(kernal)) { // now size the device offDevice.OnSize(new System.Drawing.Size(width, height)); // now create the view using (View view = new View()) { // add the new view object to the device offDevice.Add(view); // update it offDevice.Update(); // ok now create the model using (Model model = gsm.CreateAutoCADModel(kernal)) { using (Transaction nativeTrans = db.TransactionManager.StartTransaction()) { using (BlockTable bt = nativeTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable) { // now iterate through our block tables foreach (ObjectId id in bt) { if (id == BlockId) { // open the btr for read using (BlockTableRecord btr = nativeTrans.GetObject(id, OpenMode.ForRead) as BlockTableRecord) { // add the btr to the view view.Add(btr, model); try { // get the extents of the btr Extents3d extents = new Extents3d(); Vector3d buffer = new Vector3d(2, 2, 0); extents.AddBlockExtents(btr); _extMin = extents.MinPoint; _extMax = extents.MaxPoint; SetViewTo(view, db); // snap the image System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, width, height); blockBmp = view.GetSnapshot(rect); } catch { } // reset the view for the next iteration view.EraseAll(); } } } } } } offDevice.EraseAll(); } } return(blockBmp); }
//Does not update dynamic blocks public static void exportblockstosvg() { DocumentCollection docCol = Application.DocumentManager; Database localDb = docCol.MdiActiveDocument.Database; Document doc = docCol.MdiActiveDocument; CivilDocument civilDoc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; try { using (Transaction tx = localDb.TransactionManager.StartTransaction()) //using (Database symbolerDB = new Database(false, true)) { try { System.Data.DataTable fjvKomponenter = IntersectUtilities.CsvReader.ReadCsvToDataTable( @"X:\AutoCAD DRI - 01 Civil 3D\FJV Komponenter.csv", "FjvKomponenter"); //symbolerDB.ReadDwgFile(@"X:\0371-1158 - Gentofte Fase 4 - Dokumenter\01 Intern\" + // @"02 Tegninger\01 Autocad\Autocad\01 Views\0.0 Fælles\Symboler.dwg", // System.IO.FileShare.Read, true, ""); BlockTable bt = tx.GetObject(localDb.BlockTableId, OpenMode.ForRead) as BlockTable; foreach (oid Oid in bt) { BlockTableRecord btr = tx.GetObject(Oid, OpenMode.ForRead) as BlockTableRecord; if (ReadStringParameterFromDataTable(btr.Name, fjvKomponenter, "Navn", 0) != null)// && //btr.Name == "DN32 90gr twin") { Extents3d bbox = new Extents3d(); bbox.AddBlockExtents(btr); float width = ts(Math.Abs(bbox.MaxPoint.X - bbox.MinPoint.X)); float height = ts(Math.Abs(bbox.MaxPoint.Y - bbox.MinPoint.Y)); //PP = None, NP = Vertical flip, PN = Horizontal flip, NN = Vertical and Horizontal flip //NN is the same as rotating 180° Flip[] flips = new Flip[4] { Flip.PP, Flip.PN, Flip.NN, Flip.NP }; string[] flipNames = new string[] { "_PP", "_PN", "_NN", "_NP" }; for (int i = 0; i < flips.Length; i++) { Svg.SvgDocument svg = new Svg.SvgDocument() { Width = width, Height = height, ViewBox = new Svg.SvgViewBox(ts(bbox.MinPoint.X), ts(-bbox.MaxPoint.Y), width, height) }; var group = new Svg.SvgGroup(); svg.Children.Add(group); //WCS ORIGO transform matrix double[] dMatrix = new double[16] { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; Matrix3d transform = new Matrix3d(dMatrix); DrawOrDiscardEntity(btr, tx, transform, group);//, upscale); AddTransforms(group, flips[i]); svg.Write(@"X:\AutoCAD DRI - 01 Civil 3D\Svg\" + btr.Name + flipNames[i] + ".svg"); } } } } catch (System.Exception ex) { tx.Abort(); ed.WriteMessage(ex.Message); throw; } tx.Commit(); }; } catch (System.Exception ex) { ed.WriteMessage(ex.Message); } }
private void createBlock() { // создание определения блока using (var bt = CPS.DbExport.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable) { IdBtrEnd = getIdBtrEnd(bt); } // для каждой панели - копирование объектв торца с преобразование в координаты модели // список копируемых объектов торуа с привязкой к объекту блока панели для дальнейшего перемещения объектов в 0,0 в блоке торца foreach (var panelBlRef in itemLefEndsByY) { Dictionary<ObjectId, PanelBlRefExport> dictIdsEndEnts = new Dictionary<ObjectId, PanelBlRefExport>(); if (isLeftSide) { panelBlRef.PanelBtrExport.IdsEndsLeftEntity.ForEach(e => dictIdsEndEnts.Add(e, panelBlRef)); } else { panelBlRef.PanelBtrExport.IdsEndsRightEntity.ForEach(e => dictIdsEndEnts.Add(e, panelBlRef)); } ObjectIdCollection ids = new ObjectIdCollection(dictIdsEndEnts.Keys.ToArray()); using (IdMapping mapping = new IdMapping()) { CPS.DbExport.DeepCloneObjects(ids, IdBtrEnd, mapping, false); //перемещение объектов в блоке var moveMatrix = Matrix3d.Displacement(new Vector3d(0, panelBlRef.Position.Y - Position.Y, 0)); foreach (ObjectId id in ids) { var ent = mapping[id].Value.GetObject(OpenMode.ForWrite, false, true) as Entity; ent.TransformBy(moveMatrix); } } } // перемещение вех объектов торца в 0 var btr = IdBtrEnd.GetObject(OpenMode.ForRead) as BlockTableRecord; Extents3d extFull = new Extents3d(); extFull.AddBlockExtents(btr); foreach (ObjectId idEnt in btr) { using (var ent = idEnt.GetObject(OpenMode.ForWrite, false, true) as Entity) { ent.TransformBy(Matrix3d.Displacement(new Vector3d(-extFull.MinPoint.X, 0, 0))); } } ////сопоставление скопированных объектов с панелями //Dictionary<ObjectId, PanelBlRefExport> dictIdsCopyedEndEnts = new Dictionary<ObjectId, PanelBlRefExport>(); //foreach (IdPair itemIdMap in mapping) //{ // var panelBlRef = dictIdsEndEnts[itemIdMap.Key]; // dictIdsCopyedEndEnts.Add(itemIdMap.Key, panelBlRef); //} //// удаление выбранных объектов //foreach (ObjectId idEnt in ids) //{ // var ent = t.GetObject(idEnt, OpenMode.ForWrite, false, true) as Entity; // ent.Erase(); //} // вставка блока if (!IsExistsBlockEnd) { using (var blRef = new BlockReference(Position, IdBtrEnd)) { blRef.SetDatabaseDefaults(CPS.DbExport); using (var ms = SymbolUtilityServices.GetBlockModelSpaceId(CPS.DbExport).GetObject(OpenMode.ForWrite) as BlockTableRecord) { ms.AppendEntity(blRef); ms.Database.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(blRef, true); } } } }
private void createBlock() { // создание определения блока var bt = CPS.DbExport.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; IdBtrEnd = getIdBtrEnd(bt); // для каждой панели - копирование объектв торца с преобразование в координаты модели // список копируемых объектов торуа с привязкой к объекту блока панели для дальнейшего перемещения объектов в 0,0 в блоке торца foreach (var panelBlRef in itemLefEndsByY) { var dictIdsEndEnts = new Dictionary <ObjectId, PanelBlRefExport>(); if (isLeftSide) { panelBlRef.PanelBtrExport.IdsEndsLeftEntity.ForEach(e => { if (e.ObjectClass.Name == "AcDbBlockReference") { dictIdsEndEnts.Add(e, panelBlRef); } }); } else { panelBlRef.PanelBtrExport.IdsEndsRightEntity.ForEach(e => { if (e.ObjectClass.Name == "AcDbBlockReference") { dictIdsEndEnts.Add(e, panelBlRef); } }); } var ids = new ObjectIdCollection(dictIdsEndEnts.Keys.ToArray()); using (IdMapping mapping = new IdMapping()) { CPS.DbExport.DeepCloneObjects(ids, IdBtrEnd, mapping, false); //перемещение объектов в блоке var moveMatrix = Matrix3d.Displacement(new Vector3d(0, panelBlRef.Position.Y - Position.Y, 0)); foreach (ObjectId id in ids) { var ent = mapping[id].Value.GetObject(OpenMode.ForWrite, false, true) as Entity; ent.TransformBy(moveMatrix); } } } // перемещение вех объектов торца в 0 var btrEnd = IdBtrEnd.GetObject(OpenMode.ForRead) as BlockTableRecord; var extFull = new Extents3d(); extFull.AddBlockExtents(btrEnd); var tiles = new List <BlockReference>(); foreach (ObjectId idEnt in btrEnd) { var ent = idEnt.GetObject(OpenMode.ForWrite, false, true) as Entity; ent.TransformBy(Matrix3d.Displacement(new Vector3d(-extFull.MinPoint.X, 0, 0))); if (ent is BlockReference) { var blRefTile = ent as BlockReference; var blName = blRefTile.GetEffectiveName(); if (blName.Equals(Settings.Default.BlockTileName, StringComparison.OrdinalIgnoreCase)) { tiles.Add(blRefTile); } } } // Выравнивание блоков плиток if (tiles.Any()) { var xTile = tiles.First().Position.X; foreach (var item in tiles) { if (item.Position.X != xTile) { item.UpgradeOpen(); var moveMatrixTileX = Matrix3d.Displacement(new Vector3d(xTile - item.Position.X, 0, 0)); item.TransformBy(moveMatrixTileX); } } } ////сопоставление скопированных объектов с панелями //Dictionary<ObjectId, PanelBlRefExport> dictIdsCopyedEndEnts = new Dictionary<ObjectId, PanelBlRefExport>(); //foreach (IdPair itemIdMap in mapping) //{ // var panelBlRef = dictIdsEndEnts[itemIdMap.Key]; // dictIdsCopyedEndEnts.Add(itemIdMap.Key, panelBlRef); //} //// удаление выбранных объектов //foreach (ObjectId idEnt in ids) //{ // var ent = t.GetObject(idEnt, OpenMode.ForWrite, false, true) as Entity; // ent.Erase(); //} // вставка блока if (!IsExistsBlockEnd) { using (var blRef = new BlockReference(Position, IdBtrEnd)) { blRef.SetDatabaseDefaults(CPS.DbExport); using (var ms = SymbolUtilityServices.GetBlockModelSpaceId(CPS.DbExport).GetObject(OpenMode.ForWrite) as BlockTableRecord) { ms.AppendEntity(blRef); ms.Database.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(blRef, true); } } } }