public void autoBlockFit(Database db, BlockTableRecord blockTR, Transaction tr) { Point3d DownLeft_Point = new Point3d(); Point3d UpRight_Point = new Point3d(); string compareResult = string.Empty; bool started = false; try { blockTR.UpgradeOpen(); DBObjectCollection EntityInOldBlock = new DBObjectCollection(); string blockName = blockTR.Name; foreach (ObjectId entId in blockTR) { DBObject objSubBlock = (DBObject)tr.GetObject(entId, OpenMode.ForWrite); EntityInOldBlock.Add(objSubBlock); if (objSubBlock.Bounds == null) { continue; } else { if (started == false) { UpRight_Point = objSubBlock.Bounds.Value.MaxPoint; DownLeft_Point = objSubBlock.Bounds.Value.MinPoint; started = true; continue; } else { string compareMaxRes = string.Empty; string compareMinRes = string.Empty; if (objSubBlock.Bounds.Value.MaxPoint.X > UpRight_Point.X) { UpRight_Point = new Point3d(objSubBlock.Bounds.Value.MaxPoint.X, UpRight_Point.Y, UpRight_Point.Z); } if (objSubBlock.Bounds.Value.MaxPoint.Y > UpRight_Point.Y) { UpRight_Point = new Point3d(UpRight_Point.X, objSubBlock.Bounds.Value.MaxPoint.Y, UpRight_Point.Z); } if (objSubBlock.Bounds.Value.MinPoint.X < DownLeft_Point.X) { DownLeft_Point = new Point3d(objSubBlock.Bounds.Value.MinPoint.X, DownLeft_Point.Y, UpRight_Point.Z); } if (objSubBlock.Bounds.Value.MinPoint.Y < DownLeft_Point.Y) { DownLeft_Point = new Point3d(DownLeft_Point.X, objSubBlock.Bounds.Value.MinPoint.Y, UpRight_Point.Z); } } } objSubBlock.DowngradeOpen(); } //CreateBlock(blockTR.Name, EntityInOldBlock, DownLeft_Point); objectBound ob = new objectBound(DownLeft_Point, UpRight_Point); sizeEntity.Add(blockName, ob); blockTR.Origin = DownLeft_Point; blockTR.DowngradeOpen(); //minPoint和maxPoint只差为块的大小. //通过缩放解决图形大小的问题. //minPoint的位置为块离原点的距离,默认为离基点的距离. //通过平移解决插入位置的问题 /* * Polyline c1 = new Polyline(); * c1.CreatePolyCircle(new Point2d(UpRight_Point.X, UpRight_Point.Y), 5); * * Polyline c2 = new Polyline(); * c2.CreatePolyCircle(new Point2d(DownLeft_Point.X, DownLeft_Point.Y), 5); ; * * c1.ColorIndex = 200; //upright * c2.ColorIndex = 100; * //db.AddToModelSpace(c1, c2); * * try * { * blockTR.UpgradeOpen(); * blockTR.AppendEntity(c1); * blockTR.AppendEntity(c2); * tr.AddNewlyCreatedDBObject(c1, true); * tr.AddNewlyCreatedDBObject(c2, true); * blockTR.DowngradeOpen(); * * * * } * catch (Exception ee) * { * MessageBox.Show(ee + ""); * } */ } catch (Autodesk.AutoCAD.Runtime.Exception ee) { } }
/// DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); /// 代码... /// m_DocumentLock.Dispose(); /// public bool GenerateBlockPreview(string openFilePath) { bool proEnd = false; Database db = HostApplicationServices.WorkingDatabase; ObjectId spaceId = db.CurrentSpaceId;//获取当前空间(模型空间或图纸空间) Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; // 提示用户选择文件 //PromptFileNameResult result = ed.GetFileNameForOpen("请选择需要预览的文件"); //if (result.Status != PromptStatus.OK) return; // 如果未选择,则返回 //string filename = result.StringResult; // 获取带有路径的文件名 string filename = openFilePath; // 在C盘根目录下创建一个临时文件夹,用来存放文件中的块预览图标 string path = string.Empty; #if DEBUG path = StockLocation;// "C:\\Temp"; //string b= #else path = "..\\Resourse\\"; #endif if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } using (Transaction trans = db.TransactionManager.StartTransaction()) { try { DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); // 导入外部文件中的块 db.ImportBlocksFromDwg(filename); //打开块表 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead); // 循环遍历块表中的块表记录 //int i=0; foreach (ObjectId blockRecordId in bt) { // 打开 块表 记录对象 BlockTableRecord btRecord = (BlockTableRecord)trans.GetObject(blockRecordId, OpenMode.ForRead); // 如果是匿名块、布局块及没有预览图形的块,则返回 if (btRecord.IsAnonymous || btRecord.IsLayout || !btRecord.HasPreviewIcon) { continue; } Bitmap preview; try { StringBuilder str = new StringBuilder(); if (!btRecord.IsDynamicBlock) { str.Append("D"); } if (btRecord.ExtensionDictionary == null) { str.Append("E"); } // 获取块预览图案(适用于AutoCAD 2008及以下版本) //preview = BlockThumbnailHelper.GetBlockThumbanail(btr.ObjectId); preview = btRecord.PreviewIcon; // 适用于AutoCAD 2009及以上版本 preview.Save(path + "\\" + btRecord.Name + "_" + str.ToString() + ".bmp"); // 保存块预览图案 //自动整理图块比例 //listBox1.Items.Add(btRecord.Name); autoBlockFit(db, btRecord, trans); /*if (sizeEntity.ContainsKey(btRecord.Name)) * { * objectBound objectSize=new objectBound(); * objectSize= sizeEntity[btRecord.Name]; * objectSize.GetType(); * Point3d downLeft = objectSize.downLeftPoint; * double objSize = objectSize.upRightPoint.X - objectSize.downLeftPoint.X;// objectSize.upRightPoint.Y - objectSize.downLeftPoint.Y); * double scaleSize=30/objSize; * //string a = objectSize.; * //Scale(blockRecordId, downLeft, scaleSize); * }*/ //int numa = sizeEntity.Count; //重新生成块 //btr. //在图中插入 块参照 (画块) objectBound objectSize = sizeEntity[btRecord.Name]; Point3d insetP = new Point3d(Point3d.Origin.X - objectSize.downLeftPoint.X, Point3d.Origin.Y - objectSize.downLeftPoint.Y, 0); Scale3d scaleX = new Scale3d(30 / (objectSize.upRightPoint.X - objectSize.downLeftPoint.X)); Scale3d scaleY = new Scale3d(30 / (objectSize.upRightPoint.X - objectSize.downLeftPoint.Y)); Scale3d scaleE; Point3d a = btRecord.Origin; if (scaleX.X > scaleY.X) { scaleE = scaleY; } else { scaleE = scaleX; } //spaceId.InsertBlockReference("0", btRecord.Name, new Point3d(Point3d.Origin.X, Point3d.Origin.Y, 0), scaleE, 0); LayerTable lt = db.LayerTableId.GetObject(OpenMode.ForRead) as LayerTable; if (!lt.Has("1")) { LayerTableRecord ltr = new LayerTableRecord(); ltr.Name = "1"; lt.UpgradeOpen(); lt.Add(ltr); db.TransactionManager.AddNewlyCreatedDBObject(ltr, true); lt.DowngradeOpen(); } if ((btRecord.Origin.X <= objectSize.upRightPoint.X || btRecord.Origin.X >= objectSize.downLeftPoint.X) && (btRecord.Origin.Y <= objectSize.upRightPoint.Y || btRecord.Origin.Y >= objectSize.downLeftPoint.Y)) { spaceId.InsertBlockReference("0", btRecord.Name, new Point3d(Point3d.Origin.X, Point3d.Origin.Y, 0), scaleE, 0); } else { spaceId.InsertBlockReference("1", btRecord.Name, insetP, scaleE, 0); } //spaceId.InsertBlockReference("1", btRecord.Name, insetP, scaleE, 0); /* * DBText text1 = new DBText(); * text1.TextString = "layer0"; * text1.Height = 20; * text1.Position = new Point3d(Point3d.Origin.X, Point3d.Origin.Y, 0); * DBText text2 = new DBText(); * text2.TextString = "layer1"; * text2.Height = 20; * text2.Position = insetP; * db.AddToModelSpace(text1, text2); * trans.Commit(); */ } catch (Exception ee) { trans.Abort(); ed.WriteMessage("错误; " + ee.ToString()); //preview = btr.PreviewIcon; // 适用于AutoCAD 2009及以上版本 } } // trans.Commit(); int numa = sizeEntity.Count; foreach (var item in typeAndNum) { listBox1.Items.Add("type " + item.Key + "; Number: " + item.Value); } m_DocumentLock.Dispose(); fillImageList("C:\\Temp", defSize); fileListView("C:\\Temp"); proEnd = true; } catch (Exception ee) { trans.Abort(); MessageBox.Show("" + ee, "error"); } } return(proEnd); }