将模板中的布局复制到当前dwg文件的中后,视口变化了???
//根据模板中的布局创建布局,并复制模板布局中的实体到新布局中public static ObjectId CreateLayout(Database curDb, Database templateDb, string strLayoutName)
{
ObjectId layoutId = ObjectId.Null;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
DocumentLock _lock = doc.LockDocument();
using (_lock)
{
using (Transaction trans = curDb.TransactionManager.StartTransaction())
{
LayoutManager lm = LayoutManager.Current;
//检查当前布局中有没有与指定布局同名的布局,有则删除。
layoutId = lm.GetLayoutId(strLayoutName);
if (layoutId != ObjectId.Null)
{
lm.DeleteLayout(strLayoutName);
}
//创建布局
layoutId = lm.CreateLayout(strLayoutName);
Layout layout = (Layout)layoutId.GetObject(OpenMode.ForWrite);
lm.CurrentLayout = strLayoutName;
//删除布局中的实体
ObjectIdCollection entIds = LayoutTools.GetEntsInLayout(curDb,layout, true);
foreach (ObjectId entId in entIds)
{
DBObject ent = entId.GetObject(OpenMode.ForWrite) as DBObject;
ent.Erase();
}
//获得模板图中指定布局的ID和布局中的所有实体ID
ObjectIdCollection entIdsInLayout = new ObjectIdCollection();
ObjectId srcLayoutId = LayoutTools.GetLayoutId(templateDb, strLayoutName, ref entIdsInLayout);
ObjectId newBtrId = new ObjectId();
if (srcLayoutId.IsValid)
{
//复制布局中的所有实体
Layout srcLayout = (Layout)srcLayoutId.GetObject(OpenMode.ForRead);
layout.CopyFrom(srcLayout);
newBtrId = layout.BlockTableRecordId;
}
//将模板布局中的实体复制到新布局
if (entIdsInLayout.Count > 0)
{
IdMapping idmap = new IdMapping();
templateDb.WblockCloneObjects(entIdsInLayout, newBtrId, idmap, DuplicateRecordCloning.Replace, false);
}
trans.Commit();
}
}
return layoutId;
}
以上是根据模板布局创建新布局的函数。
问题:这样创建出来的布局中的视口与模板布局中的视口大小不一样(即高、宽与模板布局中视口的高、宽不一样),请高手指教,不胜感激啊!!!搞了一天了,不知为什么?是不是复制的时候有问题?
http://bbs.mjtd.com/xwb/images/bgimg/icon_logo.png 该贴已经同步到 NiNe苦瓜的微博 这是GetEntsInLayout函数代码
public static ObjectIdCollection GetEntsInLayout(Database db, Layout layout, bool bIncludeFirstViewport)
{
ObjectIdCollection entIds = new ObjectIdCollection();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
DocumentLock _lock = doc.LockDocument();
using (_lock)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
ObjectId blkDefId = layout.BlockTableRecordId;
BlockTableRecord btr = blkDefId.GetObject(OpenMode.ForRead) as BlockTableRecord;
if (btr == null)
{
return null;
}
bool bFirstViewport = true;
foreach (ObjectId entId in btr)
{
Viewport vp = entId.GetObject(OpenMode.ForRead) as Viewport;
if (vp != null && bFirstViewport)
{
if (bIncludeFirstViewport)
{
entIds.Add(entId);
bFirstViewport = false;
}
}
else
{
entIds.Add(entId);
}
if (vp != null)
{
ObjectId dictId = vp.ExtensionDictionary;
if (dictId.IsValid)
{
DBDictionary dict = dictId.GetObject(OpenMode.ForRead) as DBDictionary;
dict.TreatElementsAsHard = true;
}
}
}
trans.Commit();
}
}
return entIds;
} 这是GetLayoutId函数代码。
public static ObjectId GetLayoutId(Database db, string strLayoutName, ref ObjectIdCollection entIds)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
DocumentLock _lock = doc.LockDocument();
ObjectId layoutId = ObjectId.Null;
using (_lock)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = btrId.GetObject(OpenMode.ForRead) as BlockTableRecord;
if (btr.IsLayout)
{
Layout layout = btr.LayoutId.GetObject(OpenMode.ForRead) as Layout;
if (layout.LayoutName.CompareTo(strLayoutName) == 0)
{
layoutId = btr.LayoutId;
//获取布局所有实体(包括与模型空间关联的第一个视口)
entIds = GetEntsInLayout(db,layout, true);
break;
}
}
}
trans.Commit();
}
}
return layoutId;
} 注明:本代码摘自AutoCAD VBA&VB.Net 开发基础与实例教程(第二版)。由于本人新手,所以请各位大侠支招。谢谢。 re一下应该就可以了
页:
[1]