- 积分
- 286
- 明经币
- 个
- 注册时间
- 2009-8-24
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2009-10-21 08:58:00
|
显示全部楼层
- /// <summary>
- /// 改方法用于向当前图形中插入块
- /// </summary>
- /// <param name="dwgName">图纸的名称(除去扩展名)</param>
- /// <param name="point">插入点</param>
- /// <param name="angle">旋转角度</param>
- /// <param name="time">时间戳</param>
- /// <param name="direction">所在端子排的边</param>
- /// <param name="side">方向</param>
- private void InsertDwg(string dwgName,Point3d point,double angle,string time,string direction,string side)
- {
- string dir = direction.Substring(0,5);
- //刷新屏幕
- if (Application.DocumentManager.Count == 0)
- {
- SimpleDialog.ShowWarningDialog("请先打开一个图纸文档再进行操作");
- return;
- }
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- string blockName = "GYCAD_TerminalBom_" + dwgName;
- string dwgPath = DirConfig.SystemDwgDirectory + dwgName + ".dwg";
- using (Database blockDB = new Database(false, true))
- {
- //读取图形文件
- try
- {
- blockDB.ReadDwgFile(dwgPath, FileShare.Read, true, null);
- }
- catch
- { }
- ObjectId dwgId = new ObjectId();
- try
- {
- dwgId = db.Insert(blockName, blockDB, true);
- }
- catch
- {
- ed.WriteMessage("打开图纸错误");
- return;
- }
- using (Transaction trans = doc.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
- ObjectId blockId = dwgId;
- BlockReference br = new BlockReference(point, blockId);
- if (angle == 0.0)
- {
- if (dir == "right")
- {
- if (side == "dowm")
- {
- br.Rotation = angle;
- }
- else if (side == "up")
- {
- if (dwgName == "Triangle")
- {
- br.Rotation = angle + Math.PI * 3 / 2;
- }
- else
- {
- br.Rotation = angle + Math.PI;
- }
- }
- }
- else if (dir == "left_")
- {
- if (side == "down")
- {
- if (dwgName == "Triangle")
- {
- br.Rotation = angle + Math.PI / 2;
- }
- else
- {
- br.Rotation = angle;
- }
- }
- else if (side == "up")
- {
- if (dwgName == "Triangle")
- {
- br.Rotation = angle + Math.PI;
- }
- else
- {
- br.Rotation = angle + Math.PI;
- }
- }
- }
- }
- RegAppTable reg = (RegAppTable)trans.GetObject(db.RegAppTableId, OpenMode.ForWrite);
- if (!reg.Has(blockName))
- {
- RegAppTableRecord app = new RegAppTableRecord();
- app.Name = blockName;
- reg.Add(app);
- trans.AddNewlyCreatedDBObject(app, true);
- }
- TypedValue[] blockValues = new TypedValue[]{
- new TypedValue(Convert.ToInt16(DxfCode.ExtendedDataRegAppName),blockName),
- new TypedValue(Convert.ToInt16(DxfCode.ExtendedDataAsciiString),time),
- new TypedValue(Convert.ToInt16(DxfCode.ExtendedDataAsciiString),direction),
- new TypedValue(Convert.ToInt16(DxfCode.ExtendedDataAsciiString),side)
- };
- ResultBuffer rb = new ResultBuffer(blockValues);
- try
- {
- br.XData = rb;
- Point3d pos = br.Position;
- br.Position = point;
- br.Color = Color.FromColorIndex(ColorMethod.ByBlock, color.ColorIndex);
- }
- catch
- { }
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- btr.AppendEntity(br);
- trans.AddNewlyCreatedDBObject(br, true);
- trans.Commit();
- }
- blockDB.Dispose();
- }
- }
上面是我向图纸中插入外部块的代码,直接 br.Color = Color.FromColorIndex(ColorMethod.ByBlock, color.ColorIndex); 是不可以的,应该在什么地方改动呢?
|
|