- 积分
- 486
- 明经币
- 个
- 注册时间
- 2012-5-29
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
我想实现一个功能,将dwg文件中的所有内容移动到一层上,然后将这一层的内容导出到一个新的dwg文件中,使用如下代码,但是发现导出的文件还包含了原来dwg文件的所有层!
- public void Insert2(Data.Directions dir, string fileName)
- {
- string sTemplatePath = get(get(Autodesk.AutoCAD.ApplicationServices.Application.Preferences, "Files"), "TemplateDwgPath") as string;
- string sTemplateFile = sTemplatePath + @"\acadiso.dwt";
- string layername = "ImageIdea1" + "_" + dir.ToString() + "_" + Guid.NewGuid();
- string sTempFile = sTemplatePath + @"\" + fileName.Substring(fileName.LastIndexOf("\\") + 1); ;
- try
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- AcDb.Database db = AcDb.HostApplicationServices.WorkingDatabase;
-
- ObjectIdCollection objectids = new ObjectIdCollection();
- using (Database FileDatabase = new Database(false, false))
- {
- FileDatabase.ReadDwgFile(fileName, System.IO.FileShare.Read, false, "");
- using (Transaction tr = FileDatabase.TransactionManager.StartTransaction())
- {
- BlockTable Filebt = FileDatabase.BlockTableId.GetObject(OpenMode.ForWrite) as BlockTable;
- BlockTableRecord btrMs =
- Filebt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
- LayerTable layerTable = (LayerTable)tr.GetObject(FileDatabase.LayerTableId, OpenMode.ForWrite);
- CreateNewLayer(tr, layerTable, false, layername);
- BlockTableRecordEnumerator symbolTableEnumerator = btrMs.GetEnumerator();
- while (symbolTableEnumerator.MoveNext() != false)
- {
- Entity ent = tr.GetObject(symbolTableEnumerator.Current, OpenMode.ForRead) as Entity;
- if (ent.Layer != layername)
- {
- Entity entcopy = ent.Clone() as Entity;
- btrMs.AppendEntity(entcopy);
- tr.AddNewlyCreatedDBObject(entcopy, true);
- entcopy.Layer = layername;
- if (!objectids.Contains(entcopy.ObjectId))
- {
- objectids.Add(entcopy.ObjectId);
- }
- }
- }
- tr.Commit();
- }
- using (Database sTemplateFileDataBase = new Database(false, true))
- {
- sTemplateFileDataBase.ReadDwgFile(sTemplateFile, System.IO.FileShare.Read, true, "");
- IdMapping map = new IdMapping();
- FileDatabase.WblockCloneObjects(
- objectids,
- SymbolUtilityServices.GetBlockModelSpaceId(sTemplateFileDataBase),
- map, DuplicateRecordCloning.Replace, false);
- sTemplateFileDataBase.SaveAs(sTempFile, DwgVersion.Current);
-
- }
- }
-
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- finally
- {
- try
- {
- //if (System.IO.File.Exists(sTempFile))
- //{
- // System.IO.File.Delete(sTempFile);
- //}
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- }
- }
- static Object get(Object o, string prop)
- {
- return o.GetType().InvokeMember(prop, System.Reflection.BindingFlags.GetProperty, null, o, null);
- }
|
|