forked from Timelessww/DotNetARX2012
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutTools.cs
More file actions
157 lines (154 loc) · 7.22 KB
/
Copy pathLayoutTools.cs
File metadata and controls
157 lines (154 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace DotNetARX
{
/// <summary>
/// 布局操作类
/// </summary>
public static class LayoutTools
{
/// <summary>
/// 获取数据库的所有布局
/// </summary>
/// <param name="db">数据库对象</param>
/// <returns>返回所有布局</returns>
public static List<Layout> GetAllLayouts(this Database db)
{
List<Layout> layouts = new List<Layout>();
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
foreach (ObjectId id in bt)
{
BlockTableRecord btr = (BlockTableRecord)id.GetObject(OpenMode.ForRead);
if (btr.IsLayout && btr.Name.ToUpper() != BlockTableRecord.ModelSpace.ToUpper())
{
Layout layout = (Layout)btr.LayoutId.GetObject(OpenMode.ForRead);
layouts.Add(layout);
}
}
return layouts.OrderBy(layout => layout.TabOrder).ToList();
}
/// <summary>
/// 用于获得指定布局上的所有实体
/// </summary>
/// <param name="layout">布局对象</param>
/// <param name="bIncludeFirstViewport">是否将第一个视口包含在内</param>
/// <returns>返回所有实体的Id集合</returns>
public static ObjectIdCollection GetEntsInLayout(this Layout layout, bool bIncludeFirstViewport)
{
ObjectIdCollection entIds = new ObjectIdCollection();
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);
ObjectId dictId = vp.ExtensionDictionary;
if (dictId.IsValid)
{
DBDictionary dict = (DBDictionary)dictId.GetObject(OpenMode.ForWrite);
dict.TreatElementsAsHard = true;
}
}
return entIds;
}
/// <summary>
/// 在指定的图形数据库中寻找特定名称的布局,并且输出该布局中包含的所有实体
/// </summary>
/// <param name="db">数据库对象</param>
/// <param name="name">布局名称</param>
/// <param name="entIds">布局中包含的所有实体的Id集合</param>
/// <returns>返回布局的Id</returns>
public static ObjectId GetLayoutId(this Database db, string name, ref ObjectIdCollection entIds)
{
ObjectId layoutId = new ObjectId();
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = (BlockTableRecord)btrId.GetObject(OpenMode.ForRead);
if (btr.IsLayout)
{
Layout layout = (Layout)btr.LayoutId.GetObject(OpenMode.ForRead);
if (layout.LayoutName.CompareTo(name) == 0)
{
layoutId = btr.LayoutId;
// 获取布局中的所有实体
entIds = layout.GetEntsInLayout(true);
break;
}
}
}
return layoutId;
}
/// <summary>
/// 确保布局中的图纸显示在布局中的中间,而不需要使用缩放命令来显示
/// </summary>
/// <param name="db">数据库对象</param>
/// <param name="layoutName">布局的名称</param>
public static void CenterLayoutViewport(this Database db, string layoutName)
{
Extents3d ext = db.GetAllEntsExtent();
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(layoutName) == 0)
{
int vpIndex = 0;
ObjectId firstViewportId = new ObjectId();
ObjectId secondViewportId = new ObjectId();
foreach (ObjectId entId in btr)
{
Entity ent = entId.GetObject(OpenMode.ForWrite) as Entity;
if (ent is Viewport)
{
if (vpIndex == 0)
{
firstViewportId = entId;
vpIndex++;
}
else if (vpIndex == 1)
{
secondViewportId = entId;
}
}
}
// 布局复制过来之后得到了两个视口,第一个视口与模型空间关联,第二个视口则是在正确的位置上
if (firstViewportId.IsValid && secondViewportId.IsValid)
{
Viewport secondVP = secondViewportId.GetObject(OpenMode.ForWrite) as Viewport;
secondVP.ColorIndex = 1;
secondVP.Erase();
Viewport firstVP = firstViewportId.GetObject(OpenMode.ForWrite) as Viewport;
firstVP.CenterPoint = secondVP.CenterPoint;
firstVP.Height = secondVP.Height;
firstVP.Width = secondVP.Width;
firstVP.ColorIndex = 5;
Point3d midPt = GeTools.MidPoint(ext.MinPoint, ext.MaxPoint);
firstVP.ViewCenter = new Point2d(midPt.X, midPt.Y); ;
double xScale = secondVP.Width / ((ext.MaxPoint.X - ext.MinPoint.X) * 1.1);
double yScale = secondVP.Height / ((ext.MaxPoint.Y - ext.MinPoint.Y) * 1.1);
firstVP.CustomScale = Math.Min(xScale, yScale);
}
}
}
}
trans.Commit();
}
}
}
}