DocumentExtend.cs 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Autodesk.Revit.DB;
  7. using Autodesk.Revit.DB.Architecture;
  8. using Autodesk.Revit.DB.Structure;
  9. using Autodesk.Revit.UI.Selection;
  10. using SAGA.DotNetUtils;
  11. using SAGA.DotNetUtils.Extend;
  12. using SAGA.RevitAPI;
  13. using SAGA.RevitUtils.MEP;
  14. namespace SAGA.RevitUtils.Extends
  15. {
  16. public static class DocumentExtend
  17. {
  18. /// <summary>
  19. /// rvt项目名称
  20. /// </summary>
  21. /// <param name="doc"></param>
  22. /// <returns></returns>
  23. public static string GetShortTitle(this Document doc)
  24. {
  25. string strTitle = doc.Title;
  26. string strExtend = Path.GetExtension(strTitle);
  27. if (strExtend != null && strExtend.Length > 0)
  28. {
  29. if (!strExtend.StartsWith("."))
  30. strExtend = "." + strExtend;
  31. int intIndex = strTitle.IndexOf(strExtend);
  32. if (intIndex > 0)
  33. strTitle = strTitle.Substring(0, intIndex);
  34. }
  35. if (strTitle.Length > 12 + 3)
  36. {
  37. if (strExtend != null && strExtend.Length > 0)
  38. {
  39. return strTitle.Substring(0, 12) + "..." + strExtend;
  40. }
  41. return strTitle.Substring(0, 12) + "...";
  42. }
  43. return doc.Title;
  44. }
  45. /// <summary>
  46. /// 文档信息
  47. /// </summary>
  48. /// <param name="doc"></param>
  49. /// <returns></returns>
  50. public static string GetPathNameOrTitle(this Document doc)
  51. {
  52. //新建时为空,返回项目名称
  53. if (string.IsNullOrEmpty(doc.PathName))
  54. return doc.Title;
  55. //项目磁盘全文件名
  56. return doc.PathName;
  57. }
  58. /// <summary>
  59. /// 文档所有图元
  60. /// </summary>
  61. /// <param name="doc"></param>
  62. /// <param name="isOnlyInstance">默认只取实例图元</param>
  63. /// <returns></returns>
  64. public static List<Element> GetAllElements(this Document doc, bool isContainElementType = false)
  65. {
  66. if (isContainElementType)
  67. {
  68. FilteredElementCollector elemTypeCtor = (new FilteredElementCollector(doc)).WhereElementIsElementType();
  69. FilteredElementCollector notElemTypeCtor =
  70. (new FilteredElementCollector(doc)).WhereElementIsNotElementType();
  71. FilteredElementCollector allElementCtor = elemTypeCtor.UnionWith(notElemTypeCtor);
  72. return allElementCtor.ToElements().ToList<Element>();
  73. }
  74. else
  75. {
  76. FilteredElementCollector notElemTypeCtor =
  77. (new FilteredElementCollector(doc)).WhereElementIsNotElementType();
  78. return notElemTypeCtor.ToElements().ToList<Element>();
  79. }
  80. }
  81. /// <summary>
  82. /// 大部分几何图元,有一部分没用的
  83. /// </summary>
  84. /// <param name="doc"></param>
  85. /// <returns></returns>
  86. public static List<Element> GetGeomElements(this Document doc)
  87. {
  88. double dExtend = 1000000;
  89. dExtend = dExtend.ToApi();
  90. XYZ ptMin = new XYZ(-dExtend, -dExtend, -dExtend);
  91. XYZ ptMax = new XYZ(dExtend, dExtend, dExtend);
  92. Outline otl = new Outline(ptMin, ptMax);
  93. return doc.GetElements(otl);
  94. }
  95. /// <summary>
  96. /// 空间矩形框
  97. /// </summary>
  98. /// <param name="doc"></param>
  99. /// <param name="baseLevel"></param>
  100. /// <param name="topLevel"></param>
  101. /// <param name="baseOffset"></param>
  102. /// <param name="topOffset"></param>
  103. /// <returns></returns>
  104. public static Outline CreateOutline(this Document doc, Level baseLevel, Level topLevel, double baseOffset,
  105. double topOffset)
  106. {
  107. if (baseLevel.IsEqual(topLevel))
  108. {
  109. List<Level> listLevel = doc.GetLevels();
  110. int intIndex = listLevel.FindIndex(p => p.IsEqual(baseLevel));
  111. if (intIndex == 0)
  112. {
  113. topLevel = listLevel[intIndex + 1];
  114. }
  115. if (intIndex > 0)
  116. {
  117. baseLevel = listLevel[intIndex - 1];
  118. }
  119. }
  120. double dExtend = 9999999999d.ToApi();
  121. baseOffset = baseOffset.ToApi();
  122. topOffset = topOffset.ToApi();
  123. XYZ ptMin = new XYZ(-dExtend, -dExtend, baseLevel.Elevation + baseOffset);
  124. XYZ ptMax = new XYZ(dExtend, dExtend, topLevel.Elevation + topOffset);
  125. Outline otl = new Outline(ptMin, ptMax);
  126. return otl;
  127. }
  128. public static ElementFilter CreateAvailablyFilter(this Document doc)
  129. {
  130. if (true)
  131. {
  132. LogicalAndFilter andFilter = new LogicalAndFilter(new List<ElementFilter>
  133. {
  134. new ElementCategoryFilter(BuiltInCategory.OST_Elev, true),
  135. new ElementCategoryFilter(BuiltInCategory.OST_Cameras, true),
  136. new ElementCategoryFilter(BuiltInCategory.OST_Views, true),
  137. new ElementCategoryFilter(BuiltInCategory.OST_Viewers, true),
  138. new ElementCategoryFilter(BuiltInCategory.OST_Grids, true),
  139. new ElementCategoryFilter(BuiltInCategory.OST_SharedBasePoint, true),
  140. new ElementCategoryFilter(BuiltInCategory.OST_ProjectBasePoint, true),
  141. new ElementCategoryFilter(BuiltInCategory.OST_AnalyticalNodes, true),
  142. //new ElementCategoryFilter(BuiltInCategory.OST_WallAnalytical, true),
  143. });
  144. return andFilter;
  145. }
  146. else
  147. {
  148. LogicalOrFilter orFilter = new LogicalOrFilter(new List<ElementFilter>
  149. {
  150. new ElementCategoryFilter(BuiltInCategory.OST_StructConnections),
  151. new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionTags),
  152. //new ElementCategoryFilter(BuiltInCategory.OST_StructLocationLineControl),
  153. new ElementCategoryFilter(BuiltInCategory.OST_StructuralAnnotations),
  154. new ElementCategoryFilter(BuiltInCategory.OST_StructuralBracePlanReps),
  155. new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns),
  156. new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumnTags),
  157. //new ElementCategoryFilter(BuiltInCategory.OST_StructuralFoundation),
  158. //new ElementCategoryFilter(BuiltInCategory.OST_StructuralFoundationTags),
  159. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming),
  160. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingOpening),
  161. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingOther),
  162. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingSystem),
  163. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingTags),
  164. new ElementCategoryFilter(BuiltInCategory.OST_StructuralStiffener),
  165. new ElementCategoryFilter(BuiltInCategory.OST_StructuralStiffenerTags),
  166. new ElementCategoryFilter(BuiltInCategory.OST_StructuralTruss),
  167. new ElementCategoryFilter(BuiltInCategory.OST_Floors),
  168. new ElementCategoryFilter(BuiltInCategory.OST_FloorOpening),
  169. new ElementCategoryFilter(BuiltInCategory.OST_FloorTags),
  170. new ElementCategoryFilter(BuiltInCategory.OST_Walls),
  171. new ElementCategoryFilter(BuiltInCategory.OST_WallTags),
  172. new ElementCategoryFilter(BuiltInCategory.OST_GenericModel),
  173. new ElementCategoryFilter(BuiltInCategory.OST_GenericModel),
  174. });
  175. return orFilter;
  176. }
  177. }
  178. /// <summary>
  179. /// 两个标高之间和图元
  180. /// </summary>
  181. /// <param name="doc"></param>
  182. /// <param name="levelBase"></param>
  183. /// <param name="levelTop"></param>
  184. /// <param name="baseOffset"></param>
  185. /// <param name="topOffset"></param>
  186. /// <param name="viewDisciplineT"></param>
  187. /// <returns></returns>
  188. public static List<Element> GetAvailablyElements(this Document doc, Level levelBase, Level levelTop,
  189. double baseOffset, double topOffset, ViewDisciplineT viewDisciplineT)
  190. {
  191. Outline otl = doc.CreateOutline(levelBase, levelTop, baseOffset, topOffset);
  192. ElementFilter elementeFilter = doc.CreateAvailablyFilter(viewDisciplineT);
  193. return doc.GetElements(otl, elementeFilter);
  194. }
  195. public static ElementFilter CreateAvailablyFilter(this Document doc, ViewDisciplineT viewDisciplineT)
  196. {
  197. if (viewDisciplineT == ViewDisciplineT.Architectural)
  198. {
  199. LogicalAndFilter andFilter = new LogicalAndFilter(new List<ElementFilter>
  200. {
  201. new ElementCategoryFilter(BuiltInCategory.OST_Elev, true),
  202. new ElementCategoryFilter(BuiltInCategory.OST_Cameras, true),
  203. new ElementCategoryFilter(BuiltInCategory.OST_Views, true),
  204. new ElementCategoryFilter(BuiltInCategory.OST_Viewers, true),
  205. new ElementCategoryFilter(BuiltInCategory.OST_Grids, true),
  206. new ElementCategoryFilter(BuiltInCategory.OST_SharedBasePoint, true),
  207. new ElementCategoryFilter(BuiltInCategory.OST_ProjectBasePoint, true),
  208. new ElementCategoryFilter(BuiltInCategory.OST_AnalyticalNodes, true),
  209. //new ElementCategoryFilter(BuiltInCategory.OST_WallAnalytical, true),
  210. });
  211. return andFilter;
  212. }
  213. else
  214. {
  215. LogicalOrFilter orFilter = new LogicalOrFilter(new List<ElementFilter>
  216. {
  217. new ElementCategoryFilter(BuiltInCategory.OST_StructConnections),
  218. new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionTags),
  219. //new ElementCategoryFilter(BuiltInCategory.OST_StructLocationLineControl),
  220. new ElementCategoryFilter(BuiltInCategory.OST_StructuralAnnotations),
  221. new ElementCategoryFilter(BuiltInCategory.OST_StructuralBracePlanReps),
  222. new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns),
  223. new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumnTags),
  224. //new ElementCategoryFilter(BuiltInCategory.OST_StructuralFoundation),
  225. //new ElementCategoryFilter(BuiltInCategory.OST_StructuralFoundationTags),
  226. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming),
  227. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingOpening),
  228. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingOther),
  229. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingSystem),
  230. new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingTags),
  231. new ElementCategoryFilter(BuiltInCategory.OST_StructuralStiffener),
  232. new ElementCategoryFilter(BuiltInCategory.OST_StructuralStiffenerTags),
  233. new ElementCategoryFilter(BuiltInCategory.OST_StructuralTruss),
  234. new ElementCategoryFilter(BuiltInCategory.OST_Floors),
  235. new ElementCategoryFilter(BuiltInCategory.OST_FloorOpening),
  236. new ElementCategoryFilter(BuiltInCategory.OST_FloorTags),
  237. new ElementCategoryFilter(BuiltInCategory.OST_Walls),
  238. new ElementCategoryFilter(BuiltInCategory.OST_WallTags),
  239. new ElementCategoryFilter(BuiltInCategory.OST_GenericModel),
  240. new ElementCategoryFilter(BuiltInCategory.OST_GenericModel),
  241. });
  242. return orFilter;
  243. }
  244. }
  245. public static List<Element> GetAvailablyElements(this Document doc, Level levelBase, Level levelTop,
  246. double baseOffset, double topOffset, ViewDisciplineT viewDisciplineT, out List<Element> allElements)
  247. {
  248. allElements = null;
  249. Outline otl = doc.CreateOutline(levelBase, levelTop, baseOffset, topOffset);
  250. ElementFilter elementeFilter = doc.CreateAvailablyFilter(viewDisciplineT);
  251. allElements = doc.GetElements(otl);
  252. return doc.GetElements(otl, elementeFilter);
  253. }
  254. public static double GetWallSweepOrSteps(this Document doc, Wall wall, out double h)
  255. {
  256. h = 0;
  257. List<WallSweep> wallSweepFilter = doc.FilterElements<WallSweep>();
  258. List<FamilyInstance> instanceFilter =
  259. doc.FilterElements<FamilyInstance>().Where(p => p.Symbol.Name.Contains("坡道")).ToList();
  260. if (wallSweepFilter.Count > 0)
  261. {
  262. foreach (WallSweep wallSweep in wallSweepFilter)
  263. {
  264. List<ElementId> ids = wallSweep.GetHostIds().ToList();
  265. foreach (ElementId id in ids)
  266. {
  267. if (id.IntegerValue == wall.Id.IntegerValue)
  268. {
  269. double heigth = wallSweep.GetParameterDoubleMm(BuiltInParameter.WALL_SWEEP_OFFSET_PARAM);
  270. //foreach (FamilyInstance instance in instanceFilter)
  271. //{
  272. //if (instance.Host.Id.IntegerValue == wall.Id.IntegerValue)
  273. //{
  274. if (heigth == 0)
  275. h = heigth;
  276. else
  277. h = heigth - 0.5;
  278. //}
  279. //}
  280. }
  281. }
  282. }
  283. }
  284. return h;
  285. }
  286. public static double GetWallSweepOrStairs(this Document doc, Wall wall, out double h)
  287. {
  288. h = 0;
  289. List<WallSweep> wallSweepFilter = doc.FilterElements<WallSweep>();
  290. List<FamilyInstance> instanceFilter =
  291. doc.FilterElements<FamilyInstance>().Where(p => p.Symbol.Family.Name.Contains("台阶")).ToList();
  292. if (wallSweepFilter.Count > 0)
  293. {
  294. foreach (WallSweep wallSweep in wallSweepFilter)
  295. {
  296. List<ElementId> ids = wallSweep.GetHostIds().ToList();
  297. foreach (ElementId id in ids)
  298. {
  299. if (id.IntegerValue == wall.Id.IntegerValue)
  300. {
  301. double heigth = wallSweep.GetParameterDoubleMm(BuiltInParameter.WALL_SWEEP_OFFSET_PARAM);
  302. //foreach (FamilyInstance instance in instanceFilter)
  303. //{
  304. //if (instance.Host.Id.IntegerValue == wall.Id.IntegerValue)
  305. //{
  306. if (heigth == 0)
  307. h = heigth;
  308. else
  309. h = heigth - 0.5;
  310. //}
  311. //}
  312. }
  313. }
  314. }
  315. }
  316. return h;
  317. }
  318. public static List<Element> GetWallOrBeams(this Document doc)
  319. {
  320. ElementClassFilter wallFilter = new ElementClassFilter(typeof(Wall));
  321. ElementClassFilter instanceFilter = new ElementClassFilter(typeof(FamilyInstance));
  322. ElementStructuralTypeFilter structuralTypeFilter = new ElementStructuralTypeFilter(StructuralType.Beam);
  323. LogicalAndFilter beamFilter = new LogicalAndFilter(instanceFilter, structuralTypeFilter);
  324. LogicalOrFilter wallOrBeam = new LogicalOrFilter(wallFilter, beamFilter);
  325. return doc.GetElements(doc.ActiveView, wallOrBeam);
  326. }
  327. /// <summary>
  328. /// 分类
  329. /// </summary>
  330. /// <param name="doc"></param>
  331. /// <param name="category"></param>
  332. /// <returns></returns>
  333. public static Category GetCategory(this Document doc, BuiltInCategory category)
  334. {
  335. try
  336. {
  337. //有报错,如OST_StairsStringerCarriage 2015-10-26
  338. return doc.Settings.Categories.get_Item(category);
  339. }
  340. catch (Exception ex)
  341. {
  342. Console.WriteLine(ex.Message);
  343. }
  344. return null;
  345. }
  346. /// <summary>
  347. /// 获取某一类别的重叠元素集(注:主要针对梁柱墙板构件)
  348. /// </summary>
  349. /// <param name="doc"></param>
  350. /// <param name="category"></param>
  351. /// <returns></returns>
  352. public static List<List<Element>> GetOverlapElements(this Document doc, BuiltInCategory category)
  353. {
  354. List<List<Element>> listElementList = new List<List<Element>>();
  355. List<Element> elementList = new List<Element>();
  356. elementList = doc.FilterElements(category);
  357. //建筑柱和建筑柱重叠需要检查,建筑柱和结构柱重叠,不算重叠。注:mjy 修改BUG
  358. foreach (Element element in elementList)
  359. {
  360. List<Element> resultList = new List<Element>();
  361. switch (category)
  362. {
  363. case BuiltInCategory.OST_Columns:
  364. case BuiltInCategory.OST_StructuralColumns:
  365. FamilyInstance column = element as FamilyInstance;
  366. //double height = column.GetParameterDouble("柱截面高");
  367. //double baseOffset = column.GetParameterDouble(BuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM);
  368. //double topOffset = column.GetParameterDouble(BuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM);
  369. //XYZ point = (element as FamilyInstance).GetLocationPoint();
  370. //XYZ basePoint = point.NewZ(point.Z + baseOffset);
  371. //XYZ topPoint = point.NewZ(point.Z + baseOffset + height + topOffset);
  372. //Outline outline = new Outline(basePoint, topPoint);
  373. //BoundingBoxIntersectsFilter filter = new BoundingBoxIntersectsFilter(outline);
  374. //ElementCategoryFilter categoryFilter = new ElementCategoryFilter(category);
  375. //LogicalAndFilter andFiter = new LogicalAndFilter(filter, categoryFilter);
  376. //resultList = doc.GetElements(andFiter);
  377. ElementCategoryFilter categoryFilter = new ElementCategoryFilter(category);
  378. resultList = doc.GetElements(column, categoryFilter, 10d.ToApi());
  379. break;
  380. case BuiltInCategory.OST_StructuralFraming:
  381. Curve curve = (element as FamilyInstance).GetLocationCurve();
  382. XYZ startPoint = curve.StartPoint();
  383. XYZ endPoint = curve.EndPoint();
  384. XYZ norm = endPoint.Subtract(startPoint).Normalize();
  385. startPoint += 0.01 * norm;
  386. endPoint -= 0.01 * norm;
  387. List<Element> leftElements = doc.GetElements(startPoint);
  388. List<Element> rightElements = doc.GetElements(endPoint);
  389. leftElements.RemoveAll(p => p.Id.IsEqual(element.Id) || !p.Category.IsEqual(category));
  390. rightElements.RemoveAll(p => p.Id.IsEqual(element.Id) || !p.Category.IsEqual(category));
  391. resultList = leftElements.Concat(rightElements).ToList();
  392. break;
  393. default:
  394. resultList = element.GetIntersectElements(category, true);
  395. break;
  396. }
  397. #region 对墙进行二次处理 只保留定位线平行的墙
  398. if (category == BuiltInCategory.OST_Walls)
  399. {
  400. Wall wall = element as Wall;
  401. resultList.RemoveAll(w => !((Wall)w).Location.GetCurve().IsParallel(wall.Location.GetCurve()));
  402. }
  403. if (category.Equals(BuiltInCategory.OST_StructuralFraming))
  404. {
  405. FamilyInstance beam = element as FamilyInstance;
  406. Curve beamCurve = beam.GetLocationCurve();
  407. resultList.RemoveAll(p => !((FamilyInstance)p).GetLocationCurve().IsParallel(beamCurve));
  408. }
  409. #endregion
  410. if (resultList.Count <= 1) continue;
  411. #region 循环判断resultList的元素是否存在于listElementList,若存在则添加resultList中元素到对应序列中,若不存在则把resultList作为新项添加到listElementList中
  412. if (listElementList.Exists(p => p.Exists(q => resultList.Exists(r => r.IsEqual(q)))))
  413. {
  414. foreach (List<Element> elList in listElementList)
  415. {
  416. if (resultList.Exists(p => elList.Exists(q => q.IsEqual(p))))
  417. {
  418. elList.AddRange(resultList.FindAll(p => !elList.Exists(q => p.IsEqual(q))).ToArray());
  419. break;
  420. }
  421. }
  422. }
  423. else listElementList.Add(resultList);
  424. #endregion
  425. }
  426. return listElementList;
  427. }
  428. public static DisplayUnitType GetDisplayUnitType(this Document doc)
  429. {
  430. try
  431. {
  432. UnitType unittype = UnitType.UT_Length;
  433. Units projectUnit = doc.GetUnits();
  434. FormatOptions formatOption = projectUnit.GetFormatOptions(unittype);
  435. return formatOption.DisplayUnits;
  436. }
  437. catch
  438. {
  439. return DisplayUnitType.DUT_DECIMAL_FEET;
  440. }
  441. }
  442. /// <summary>
  443. /// 过滤类型对象
  444. /// </summary>
  445. /// <typeparam name="T"></typeparam>
  446. /// <param name="doc"></param>
  447. /// <param name="view"></param>
  448. /// <returns></returns>
  449. public static List<T> FilterElements<T>(this Document doc, View view = null) where T : class
  450. {
  451. FilteredElementCollector collector = null;
  452. if (view == null)
  453. {
  454. collector = new FilteredElementCollector(doc);
  455. }
  456. else
  457. {
  458. collector = new FilteredElementCollector(doc, view.Id);
  459. }
  460. return collector.OfClass(typeof(T)).ToList<T>();
  461. }
  462. /// <summary>
  463. /// 过滤类型对象
  464. /// </summary>
  465. /// <typeparam name="T"></typeparam>
  466. /// <param name="doc"></param>
  467. /// <param name="condition"></param>
  468. /// <returns></returns>
  469. public static List<T> FilterElements<T>(this Document doc, Predicate<T> condition) where T : Element
  470. {
  471. List<T> listItems = doc.FilterElements<T>();
  472. List<T> listRtn = new List<T>();
  473. foreach (T item in listItems)
  474. {
  475. if (condition(item))
  476. listRtn.Add(item);
  477. }
  478. return listRtn;
  479. }
  480. /// <summary>
  481. /// 过滤类型对象
  482. /// </summary>
  483. /// <typeparam name="T"></typeparam>
  484. /// <param name="doc"></param>
  485. /// <param name="filter"></param>
  486. /// <returns></returns>
  487. public static List<T> FilterElements<T>(this Document doc, ElementFilter filter) where T : Element
  488. {
  489. var collector = new FilteredElementCollector(doc);
  490. //collector = collector.OfClass(typeof(T));
  491. collector = collector.WherePasses(filter);
  492. return collector.ToList<T>();
  493. }
  494. /// <summary>
  495. /// 类型过滤图元
  496. /// </summary>
  497. /// <param name="doc"></param>
  498. /// <param name="type"></param>
  499. /// <returns></returns>
  500. public static List<Element> FilterElements(this Document doc, Type type)
  501. {
  502. var collector = new FilteredElementCollector(doc);
  503. return collector.OfClass(type).ToList<Element>();
  504. }
  505. /// <summary>
  506. /// 过滤器过滤图元
  507. /// </summary>
  508. /// <param name="doc"></param>
  509. /// <param name="filter"></param>
  510. /// <returns></returns>
  511. public static List<Element> FilterElements(this Document doc, ElementFilter filter)
  512. {
  513. var collector = new FilteredElementCollector(doc);
  514. return collector.WherePasses(filter).ToList<Element>();
  515. }
  516. /// <summary>
  517. /// 过滤器过滤图元
  518. /// </summary>
  519. /// <param name="doc"></param>
  520. /// <param name="filter"></param>
  521. /// <returns></returns>
  522. public static List<Element> FilterElements(this Document doc, IEnumerable<Element> elements, ElementFilter filter)
  523. {
  524. var collector = new FilteredElementCollector(doc, elements.Select(t => t.Id).ToList());
  525. return collector.WherePasses(filter).ToList<Element>();
  526. }
  527. /// <summary>
  528. /// 分类过滤实例图元
  529. ///</summary>
  530. /// <param name="doc"></param>
  531. /// <param name="bic"></param>
  532. /// <returns></returns>
  533. public static List<Element> FilterElements(this Document doc, BuiltInCategory bic)
  534. {
  535. List<Element> listRtn = null;
  536. var filter = new ElementCategoryFilter(bic);
  537. var collector = new FilteredElementCollector(doc);
  538. listRtn = collector.WherePasses(filter).WhereElementIsNotElementType().ToList<Element>();
  539. return listRtn;
  540. }
  541. /// <summary>
  542. /// 分类过滤类型图元
  543. /// </summary>
  544. /// <param name="doc"></param>
  545. /// <param name="bic"></param>
  546. /// <returns></returns>
  547. public static List<ElementType> FilterElementTypes(this Document doc, BuiltInCategory bic)
  548. {
  549. List<ElementType> listRtn = null;
  550. var filter = new ElementCategoryFilter(bic);
  551. var collector = new FilteredElementCollector(doc);
  552. listRtn = collector.WherePasses(filter).WhereElementIsElementType().ToList<ElementType>();
  553. return listRtn;
  554. }
  555. /// <summary>
  556. /// 获取类型
  557. /// </summary>
  558. /// <typeparam name="T"></typeparam>
  559. /// <param name="doc"></param>
  560. /// <param name="bic"></param>
  561. /// <returns></returns>
  562. public static List<T> FilterElementTypes<T>(this Document doc, BuiltInCategory bic)
  563. {
  564. List<T> listRtn = null;
  565. var filter = new ElementCategoryFilter(bic);
  566. var collector = new FilteredElementCollector(doc);
  567. listRtn = collector.WherePasses(filter).WhereElementIsElementType().ToList<T>();
  568. return listRtn;
  569. }
  570. public static List<FamilyInstance> FilterInstances(this Document doc, IAllowElement allowElement,
  571. DrivenTypes drivenType)
  572. {
  573. List<FamilyInstance> items = null;
  574. var collector = new FilteredElementCollector(doc);
  575. switch (drivenType)
  576. {
  577. case DrivenTypes.Curve:
  578. items = collector.WherePasses(new ElementIsCurveDrivenFilter(false))
  579. .OfClass(typeof(FamilyInstance)).ToList<FamilyInstance>();
  580. break;
  581. case DrivenTypes.Point:
  582. default:
  583. break;
  584. }
  585. if (items != null) return items.Where(allowElement.IsAllow).ToList();
  586. return null;
  587. }
  588. /// <summary>
  589. /// id返回图元
  590. /// </summary>
  591. /// <param name="doc"></param>
  592. /// <param name="intId"></param>
  593. /// <returns></returns>
  594. public static Element GetElement(this Document doc, int intId)
  595. {
  596. try
  597. {
  598. var id = new ElementId(intId);
  599. return doc.GetElement(id);
  600. }
  601. catch (Exception e)
  602. {
  603. return null;
  604. }
  605. }
  606. /// <summary>
  607. ///
  608. /// </summary>
  609. /// <typeparam name="T"></typeparam>
  610. /// <param name="doc"></param>
  611. /// <param name="id"></param>
  612. /// <returns></returns>
  613. public static T GetElementT<T>(this Document doc, ElementId id) where T : Element
  614. {
  615. return doc.GetElement(id) as T;
  616. }
  617. /// <summary>
  618. ///
  619. /// </summary>
  620. /// <typeparam name="T"></typeparam>
  621. /// <param name="doc"></param>
  622. /// <param name="name"></param>
  623. /// <returns></returns>
  624. public static T GetElementT<T>(this Document doc, string name) where T : Element
  625. {
  626. return doc.GetElement(name) as T;
  627. }
  628. /// <summary>
  629. /// 获取图元
  630. /// </summary>
  631. /// <typeparam name="T"></typeparam>
  632. /// <param name="doc"></param>
  633. /// <returns></returns>
  634. public static List<T> GetElements<T>(this Document doc) where T : Element
  635. {
  636. return doc.FilterElements<T>();
  637. }
  638. /// <summary>
  639. /// 根据名称判断某类图元是否存在 2015-11-25
  640. /// </summary>
  641. /// <typeparam name="T"></typeparam>
  642. /// <param name="doc"></param>
  643. /// <param name="strName"></param>
  644. /// <returns></returns>
  645. public static bool IsExistElement<T>(this Document doc, string strName) where T : Element
  646. {
  647. return doc.GetElements<T>().Exists(p => p.Name == strName);
  648. }
  649. /// <summary>
  650. /// 分类图元
  651. /// </summary>
  652. /// <typeparam name="T"></typeparam>
  653. /// <param name="doc"></param>
  654. /// <param name="bic"></param>
  655. /// <returns></returns>
  656. public static List<T> GetElements<T>(this Document doc, BuiltInCategory bic) where T : Element
  657. {
  658. var filter = new ElementCategoryFilter(bic);
  659. return doc.FilterElements<T>(filter);
  660. }
  661. /// <summary>
  662. /// 相交的图元,慢过滤
  663. /// </summary>
  664. /// <typeparam name="T"></typeparam>
  665. /// <param name="doc"></param>
  666. /// <param name="elem"></param>
  667. /// <returns></returns>
  668. public static List<T> GetElements<T>(this Document doc, Element elem) where T : Element
  669. {
  670. var collector = new FilteredElementCollector(doc);
  671. var filter = new ElementIntersectsElementFilter(elem);
  672. return collector.WherePasses(filter).ToList<T>();
  673. }
  674. /// <summary>
  675. /// 获取图元
  676. /// </summary>
  677. /// <param name="doc"></param>
  678. /// <param name="type"></param>
  679. /// <returns></returns>
  680. public static List<Element> GetElements(this Document doc, Type type)
  681. {
  682. return doc.FilterElements(type);
  683. }
  684. /// <summary>
  685. /// 通过类型获取图元
  686. /// </summary>
  687. /// <param name="doc"></param>
  688. /// <param name="eleType"></param>
  689. /// <returns></returns>
  690. public static List<Element> GetElements(this Document doc, ElementType eleType)
  691. {
  692. List<Element> listRtn;
  693. BuiltInCategory cat = eleType.GetCategory();
  694. //不是所有的图元都有Category
  695. if (cat != BuiltInCategory.INVALID)
  696. {
  697. listRtn = doc.FilterElements(cat);
  698. }
  699. else
  700. {
  701. listRtn =
  702. doc.GetAllElements(true).FindAll(p => p.GetElementType() != null && p.GetElementType().IsEqual(eleType));
  703. }
  704. return listRtn;
  705. }
  706. /// <summary>
  707. /// 包含某一参数的图元
  708. /// </summary>
  709. /// <typeparam name="T"></typeparam>
  710. /// <param name="doc"></param>
  711. /// <param name="para"></param>
  712. /// <returns></returns>
  713. public static List<T> GetElements<T>(this Document doc, BuiltInParameter para) where T : Element
  714. {
  715. return doc.GetElements<T>().FindAll(p => p.ExsitsParameter(para));
  716. }
  717. /// <summary>
  718. /// 与当前相交的图元,快速过滤
  719. /// </summary>
  720. /// <param name="doc"></param>
  721. /// <param name="elem"></param>
  722. /// <returns></returns>
  723. public static List<Element> GetElements(this Document doc, Element elem, double tolerance = 0)
  724. {
  725. BoundingBoxXYZ box = elem.get_BoundingBox(null);
  726. return box == null ? new List<Element>() : GetElements(doc, box, tolerance);
  727. }
  728. /// <summary>
  729. /// 相交的图元
  730. /// </summary>
  731. /// <param name="doc"></param>
  732. /// <param name="box"></param>
  733. /// <returns></returns>
  734. public static List<Element> GetElements(this Document doc, BoundingBoxXYZ box, double tolerance = 0)
  735. {
  736. var otl = new Outline(box.Min, box.Max);
  737. return doc.GetElements(otl, tolerance);
  738. }
  739. /// <summary>
  740. /// 根据 BoundingBox获取特定类别相交图元
  741. /// </summary>
  742. /// <param name="doc"></param>
  743. /// <param name="box"></param>
  744. /// <param name="category">类别</param>
  745. /// <param name="tolerance">误差值</param>
  746. /// <returns></returns>
  747. public static List<Element> GetElements(this Document doc, BoundingBoxXYZ box, BuiltInCategory category,
  748. double tolerance = 0)
  749. {
  750. Outline outline = new Outline(box.Min, box.Max);
  751. BoundingBoxIntersectsFilter boundFilter = tolerance.IsEqual(0)
  752. ? new BoundingBoxIntersectsFilter(outline)
  753. : new BoundingBoxIntersectsFilter(outline, tolerance);
  754. ElementCategoryFilter categoryFilter = new ElementCategoryFilter(category);
  755. LogicalAndFilter andFilter = new LogicalAndFilter(boundFilter, categoryFilter);
  756. return doc.GetElements(andFilter);
  757. }
  758. /// <summary>
  759. /// 根据 获取BoundingBox内特定类别图元(包含)
  760. /// </summary>
  761. /// <param name="doc"></param>
  762. /// <param name="box"></param>
  763. /// <param name="category">类别</param>
  764. /// <returns></returns>
  765. public static List<Element> GetElements(this Document doc, BoundingBoxXYZ box, BuiltInCategory category)
  766. {
  767. Outline outline = new Outline(box.Min, box.Max);
  768. //BoundingBoxIntersectsFilter boundFilter = tolerance.IsEqual(0)
  769. // ? new BoundingBoxIntersectsFilter(outline)
  770. // : new BoundingBoxIntersectsFilter(outline, tolerance);
  771. ElementCategoryFilter categoryFilter = new ElementCategoryFilter(category);
  772. //return doc.GetElements(andFilter);
  773. // Create a BoundingBoxIsInside filter for Outline
  774. BoundingBoxIsInsideFilter filter = new BoundingBoxIsInsideFilter(outline);
  775. LogicalAndFilter andFilter = new LogicalAndFilter(filter, categoryFilter);
  776. // Apply the filter to the elements in the active document
  777. // This filter excludes all objects derived from View and objects derived from ElementType
  778. FilteredElementCollector collector = new FilteredElementCollector(doc);
  779. IList<Element> elements = collector.WherePasses(andFilter).ToElements();
  780. // Find walls outside BoundingBox: use an inverted filter to match elements
  781. // Use shortcut command OfClass() to find walls only
  782. BoundingBoxIsInsideFilter outsideFilter = new BoundingBoxIsInsideFilter(outline, true); // inverted filter
  783. collector = new FilteredElementCollector(doc);
  784. IList<Element> outsideFounds = collector.OfClass(typeof(Wall)).WherePasses(outsideFilter).ToElements();
  785. return elements.ToList();
  786. }
  787. /// <summary>
  788. /// 根据 BoundingBox获取指定类形相交图元
  789. /// </summary>
  790. /// <param name="doc"></param>
  791. /// <param name="box"></param>
  792. /// <param name="type">指定类型</param>
  793. /// <param name="tolerance"></param>
  794. /// <returns></returns>
  795. public static List<Element> GetElements(this Document doc, BoundingBoxXYZ box, Type type, double tolerance = 0)
  796. {
  797. Outline outline = new Outline(box.Min, box.Max);
  798. BoundingBoxIntersectsFilter boundFilter = tolerance.IsEqual(0)
  799. ? new BoundingBoxIntersectsFilter(outline)
  800. : new BoundingBoxIntersectsFilter(outline, tolerance);
  801. return doc.GetElements(boundFilter).FindAll(p => p.GetType() == type);
  802. }
  803. /// <summary>
  804. /// 相交的图元
  805. /// 通过此方法可以找到相连接图元,如两个首尾相接的墙
  806. /// </summary>
  807. /// <param name="doc"></param>
  808. /// <param name="otl"></param>
  809. /// <returns></returns>
  810. public static List<Element> GetElements(this Document doc, Outline otl, double tolerance = 0)
  811. {
  812. if (tolerance.IsEqual(0))
  813. {
  814. var boundFilter = new BoundingBoxIntersectsFilter(otl);
  815. return doc.FilterElements(boundFilter);
  816. }
  817. else
  818. {
  819. var boundFilter = new BoundingBoxIntersectsFilter(otl, tolerance);
  820. return doc.FilterElements(boundFilter);
  821. }
  822. }
  823. /// <summary>
  824. /// 获取图元,快速过滤
  825. /// </summary>
  826. /// <param name="doc"></param>
  827. /// <param name="elem"></param>
  828. /// <param name="filter"></param>
  829. /// <param name="dSub"></param>
  830. /// <returns></returns>
  831. public static List<Element> GetElements(this Document doc, Element elem, ElementFilter filter, double dSub = 0)
  832. {
  833. var box = elem.get_BoundingBox(null);
  834. if (box == null)
  835. return new List<Element>();
  836. var vectSub = new XYZ(dSub, dSub, dSub);
  837. var otl = new Outline(box.Min.Add(vectSub), box.Max.Subtract(vectSub));
  838. return doc.GetElements(otl, filter);
  839. }
  840. /// <summary>
  841. /// 获取图元,快速过滤
  842. /// </summary>
  843. /// <param name="doc"></param>
  844. /// <param name="otl"></param>
  845. /// <param name="filter"></param>
  846. /// <returns></returns>
  847. public static List<Element> GetElements(this Document doc, Outline otl, ElementFilter filter)
  848. {
  849. var boundFilter = new BoundingBoxIntersectsFilter(otl);
  850. var allFilter = new LogicalAndFilter(boundFilter, filter);
  851. return doc.FilterElements(allFilter);
  852. }
  853. /// <summary>
  854. /// 过滤器过滤图元
  855. /// </summary>
  856. /// <param name="doc"></param>
  857. /// <param name="filter"></param>
  858. /// <returns></returns>
  859. public static List<Element> GetElements(this Document doc, IEnumerable<Element> elements, ElementFilter filter)
  860. {
  861. return doc.FilterElements(elements, filter);
  862. }
  863. /// <summary>
  864. /// 过滤器过滤图元
  865. /// </summary>
  866. /// <param name="doc"></param>
  867. /// <param name="filter"></param>
  868. /// <returns></returns>
  869. public static List<Element> GetElements(this Document doc, ElementFilter filter)
  870. {
  871. return doc.FilterElements(filter);
  872. }
  873. public static List<Level> GetLevels(this Document doc)
  874. {
  875. List<Level> mLevels = null;
  876. mLevels = doc.FilterElements<Level>();
  877. mLevels.Sort(new CommonComparer<Level>((x, y) => { return x.Elevation.CompareTo(y.Elevation); }));
  878. return mLevels;
  879. }
  880. /// <summary>
  881. /// 返回激活层之间的所有Element
  882. /// </summary>
  883. /// <param name="doc"></param>
  884. /// <returns></returns>
  885. public static List<Element> GetElementsInActiveView(this Document doc)
  886. {
  887. var levelList = doc.GetLevels();
  888. var levelData = levelList.FindIndex(m => m.Id == doc.ActiveView.GenLevel.Id);
  889. doc.GetElements(doc.ActiveView.GenLevel);
  890. Level topLevel = null;
  891. if (levelData < levelList.Count - 1)
  892. {
  893. topLevel = levelList[levelData + 1];
  894. }
  895. List<Element> elements = new List<Element>();
  896. if (topLevel != null)
  897. {
  898. elements = doc.GetElements(doc.ActiveView.GenLevel, topLevel);
  899. }
  900. return elements;
  901. }
  902. public static List<Element> GetElements(this Document doc, IEnumerable<ElementId> ids)
  903. {
  904. List<Element> elements = new List<Element>();
  905. foreach (ElementId id in ids)
  906. {
  907. Element element = doc.GetElement(id);
  908. if (element != null)
  909. {
  910. elements.Add(element);
  911. }
  912. }
  913. return elements;
  914. }
  915. /// <summary>
  916. /// 框选楼板用
  917. /// </summary>
  918. /// <param name="doc"></param>
  919. /// <param name="pickedBox">切记,PickedBox的Z轴有可能不是当前平面Z轴 </param>
  920. /// <param name="filter"></param>
  921. /// <param name="dZOffset"></param>
  922. /// <param name="dZExtend"></param>
  923. /// <returns></returns>
  924. public static List<Element> GetElements(this Document doc, PickedBox pickedBox, ElementFilter filter,
  925. double dZOffset, double dZExtend = 0)
  926. {
  927. ElementFilter ef = null;
  928. double x1 = Math.Max(pickedBox.Max.X, pickedBox.Min.X);
  929. double y1 = Math.Max(pickedBox.Max.Y, pickedBox.Min.Y);
  930. double x2 = Math.Min(pickedBox.Max.X, pickedBox.Min.X);
  931. double y2 = Math.Min(pickedBox.Max.Y, pickedBox.Min.Y);
  932. double z = pickedBox.Min.Z + dZOffset.ToApi();
  933. double dZExtendDown = 110d.ToApi(); //Z方向范围
  934. double dZExtendUp = 10d.ToApi(); //Z方向范围
  935. if (!dZExtend.IsZero(0))
  936. {
  937. dZExtendDown = dZExtend.ToApi();
  938. dZExtendUp = dZExtend.ToApi();
  939. }
  940. XYZ ptMin = new XYZ(x2, y2, z - dZExtendDown);
  941. XYZ ptMax = new XYZ(x1, y1, z + dZExtendUp);
  942. Outline otl = new Outline(ptMin, ptMax);
  943. if (filter != null)
  944. {
  945. ef = new LogicalAndFilter(filter, new BoundingBoxIntersectsFilter(otl));
  946. }
  947. else
  948. {
  949. ef = new BoundingBoxIntersectsFilter(otl);
  950. }
  951. return doc.FilterElements(ef);
  952. }
  953. /// <summary>
  954. /// 对应"在视图中可见的"对象
  955. /// </summary>
  956. /// <param name="doc"></param>
  957. /// <param name="view"></param>
  958. /// <param name="filter"></param>
  959. /// <returns></returns>
  960. public static List<Element> GetElements(this Document doc, View view, ElementFilter filter = null)
  961. {
  962. FilteredElementCollector collector = new FilteredElementCollector(doc, view.Id);
  963. if (filter == null)
  964. {
  965. return collector.ToElements().ToList<Element>();
  966. }
  967. else
  968. {
  969. return collector.WherePasses(filter).ToElements().ToList<Element>();
  970. }
  971. }
  972. /// <summary>
  973. /// 返回视图中创建的对象,如类型标记等,可获取隐藏对象。
  974. /// </summary>
  975. /// <param name="doc"></param>
  976. /// <param name="viewId"></param>
  977. /// <returns></returns>
  978. public static List<Element> GetElements(this Document doc, ElementId viewId)
  979. {
  980. FilteredElementCollector collector = new FilteredElementCollector(doc);
  981. return collector.OwnedByView(viewId).ToList<Element>();
  982. }
  983. /// <summary>
  984. /// 主要是层间对象
  985. /// </summary>
  986. /// <param name="doc"></param>
  987. /// <param name="baseLevel"></param>
  988. /// <param name="topLevel"></param>
  989. /// <returns></returns>
  990. public static List<Element> GetElements(this Document doc, Level baseLevel, Level topLevel)
  991. {
  992. Outline otl = doc.CreateOutline(baseLevel, topLevel, 10, -10);
  993. return doc.GetElements(otl);
  994. }
  995. /// <summary>
  996. /// 两标高之间的图元
  997. /// </summary>
  998. /// <param name="doc"></param>
  999. /// <param name="baseLevel">下标高</param>
  1000. /// <param name="topLevel">上标高</param>
  1001. /// <param name="baseOffset">下偏移</param>
  1002. /// <param name="topOffset">上偏移</param>
  1003. /// <returns></returns>
  1004. public static List<Element> GetElements(this Document doc, Level baseLevel, Level topLevel, double baseOffset,
  1005. double topOffset)
  1006. {
  1007. Outline otl = doc.CreateOutline(baseLevel, topLevel, baseOffset, topOffset);
  1008. return doc.GetElements(otl);
  1009. }
  1010. /// <summary>
  1011. /// 点获取图元
  1012. /// 获取包含指定点的所有图元
  1013. /// </summary>
  1014. /// <param name="doc"></param>
  1015. /// <param name="pt"></param>
  1016. /// <param name="tolerance">误差值</param>
  1017. /// <returns></returns>
  1018. /// ps:编译器只识别9位小数,而该方法默认识别13位小数,故需要指定误差值
  1019. public static List<Element> GetElements(this Document doc, XYZ pt, double tolerance = 0)
  1020. {
  1021. return doc.FilterElements(new BoundingBoxContainsPointFilter(pt, tolerance));
  1022. }
  1023. /// <summary>
  1024. /// 获取图元
  1025. /// </summary>
  1026. /// <param name="doc"></param>
  1027. /// <param name="pt"></param>
  1028. /// <param name="filter"></param>
  1029. /// <returns></returns>
  1030. public static List<Element> GetElements(this Document doc, XYZ pt, ElementFilter filter)
  1031. {
  1032. List<Element> listRtn = new List<Element>();
  1033. ElementFilter ef = new LogicalAndFilter(filter, new BoundingBoxContainsPointFilter(pt));
  1034. listRtn = doc.FilterElements(ef);
  1035. return listRtn;
  1036. }
  1037. /// <summary>
  1038. /// 获取文字类型
  1039. /// 由于符号用到 所以 从单行/多行文字中提出
  1040. /// </summary>
  1041. /// <param name="doc"></param>
  1042. /// <param name="strFont">文字字体</param>
  1043. /// <param name="dFontHeight">字高</param>
  1044. /// <param name="dHwRatio">宽度系数</param>
  1045. /// <returns>返回文字类型</returns>
  1046. public static TextNoteType GetTextNodeType(this Document doc, string strFont, double dFontHeight,
  1047. double dHwRatio, string leaderArrowheadName = "实心箭头 30 度")
  1048. {
  1049. List<TextNoteType> types = doc.FilterElements<TextNoteType>();
  1050. TextNoteType rtnType = null;
  1051. string strTypename = string.Format("文字 {0} mm * {1} {2}", new object[] { dFontHeight, dHwRatio, strFont });
  1052. if (types.Count > 0)
  1053. {
  1054. foreach (TextNoteType textType in types)
  1055. {
  1056. if (textType.Name == strTypename)
  1057. {
  1058. rtnType = textType;
  1059. break;
  1060. }
  1061. }
  1062. if (rtnType == null)
  1063. {
  1064. rtnType = types[0].DuplicateT<TextNoteType>(strTypename);
  1065. //宽度系数
  1066. rtnType.SetParameter(BuiltInParameter.TEXT_WIDTH_SCALE, dHwRatio);
  1067. //文字大小
  1068. rtnType.SetParameter(BuiltInParameter.TEXT_SIZE, dFontHeight.ToApi());
  1069. //文字字体
  1070. rtnType.SetParameter(BuiltInParameter.TEXT_FONT, strFont);
  1071. //背景-透明
  1072. rtnType.SetParameter(BuiltInParameter.TEXT_BACKGROUND, 1);
  1073. //引线/边界偏移量
  1074. rtnType.SetParameter(BuiltInParameter.LEADER_OFFSET_SHEET, 0.0);
  1075. //引线箭头
  1076. List<ElementType> listEleType =
  1077. doc.FilterElements<ElementType>()
  1078. .Where(d => d.Name.Equals(leaderArrowheadName))
  1079. .ToList<ElementType>();
  1080. if ((listEleType != null) && (listEleType.Count > 0))
  1081. {
  1082. rtnType.SetParameter(BuiltInParameter.LEADER_ARROWHEAD, listEleType[0].Id);
  1083. }
  1084. }
  1085. }
  1086. return rtnType;
  1087. }
  1088. public static TextNoteType GetTextNodeType(this Document doc, string strTypeName)
  1089. {
  1090. TextNoteType textNodeType = doc.FindTextNodeType(strTypeName, true);
  1091. return textNodeType;
  1092. }
  1093. public static TextNoteType FindTextNodeType(this Document doc, string strTypeName)
  1094. {
  1095. return doc.FindTextNodeType(strTypeName, false);
  1096. }
  1097. /// <summary>
  1098. /// 文字类型
  1099. /// </summary>
  1100. /// <param name="doc"></param>
  1101. /// <returns></returns>
  1102. public static List<TextNoteType> GetTextNoteTypeSet(this Document doc)
  1103. {
  1104. return doc.FilterElements<TextNoteType>();
  1105. }
  1106. public static TextNoteType FindTextNodeType(this Document doc, string strTypeName, bool notFindReturnDefault)
  1107. {
  1108. TextNoteType returnDefault = null;
  1109. List<TextNoteType> textNodeTypes = doc.GetTextNoteTypeSet();
  1110. foreach (TextNoteType textNodeType in textNodeTypes)
  1111. {
  1112. if (textNodeType.Name == strTypeName)
  1113. {
  1114. return textNodeType;
  1115. }
  1116. if (returnDefault == null)
  1117. returnDefault = textNodeType;
  1118. }
  1119. if (notFindReturnDefault)
  1120. return returnDefault;
  1121. return null;
  1122. }
  1123. /// <summary>
  1124. /// 标注类型
  1125. /// </summary>
  1126. /// <param name="doc"></param>
  1127. /// <returns></returns>
  1128. public static List<DimensionType> GetDimensionTypeSet(this Document doc)
  1129. {
  1130. return doc.FilterElements<DimensionType>();
  1131. }
  1132. /// <summary>
  1133. /// 获取由用户自己配置的标注样式
  1134. /// 注:根据文档要求,项目中所有用到的标注样式都从该方法获取
  1135. /// </summary>
  1136. /// <param name="doc"></param>
  1137. /// <returns></returns>
  1138. //public static DimensionType GetDimensionType(this Document doc)
  1139. //{
  1140. // return doc.GetDimensionType(null);
  1141. //}
  1142. /// <summary>
  1143. /// 获取标注默认配置
  1144. /// </summary>
  1145. /// <returns></returns>
  1146. private static string GetDimensionDefConfig()
  1147. {
  1148. /*
  1149. * 根据文档<<编辑工具功能设定>>要求,标注使用以下默认值 根据谭工要求,重新设定默认值
  1150. * 两侧出头:2.5(mm) 两侧出头:1(mm)
  1151. * 终端出头:2.5(mm) 终端出头:2(mm)
  1152. * 引出线长:3.5(mm) 引出线长:3.5(mm)
  1153. * 字线间距:1.5(mm) 字线间距:0.5(mm)
  1154. * 粗线宽度:4(mm) 粗线宽度:6(mm)
  1155. * 粗线长度:3(mm) 粗线长度:1.24(mm)
  1156. * 标注字高:3(mm) 标注字高:3.5(mm)
  1157. * 标注字体:SAGArevit 标注字体:Dotum
  1158. * 标注颜色:无 标注颜色:绿
  1159. */
  1160. string strDefault = "Dotum"; // RevitUtils.GetSAGAFontName();
  1161. string defaultConfig = ",1.24,6,3.5,0.5,2,3.5," + strDefault + ",1,65280,";
  1162. return defaultConfig;
  1163. }
  1164. /// <summary>
  1165. /// 获取一个标注的参数
  1166. /// </summary>
  1167. /// <param name="type"></param>
  1168. /// <returns></returns>
  1169. public static string GetDimensionConfig(this DimensionType type)
  1170. {
  1171. double cxcd =
  1172. type.GetParameterElement(BuiltInParameter.DIM_LEADER_ARROWHEAD)
  1173. .GetParameterDoubleMm(BuiltInParameter.ARROW_SIZE);
  1174. double cxkd = type.GetParameterInteger(BuiltInParameter.TICK_MARK_PEN);
  1175. double bzzg = type.GetParameterDoubleMm(BuiltInParameter.TEXT_SIZE);
  1176. double zxjj = type.GetParameterDoubleMm(BuiltInParameter.TEXT_DIST_TO_LINE);
  1177. double zdct = type.GetParameterDoubleMm(BuiltInParameter.WITNS_LINE_EXTENSION);
  1178. double ycxc = type.GetParameterDoubleMm(BuiltInParameter.DIM_WITNS_LINE_EXTENSION_BELOW);
  1179. string strDefault = type.GetParameterString(BuiltInParameter.TEXT_FONT);
  1180. double lcct = type.GetParameterDoubleMm(BuiltInParameter.DIM_LINE_EXTENSION);
  1181. double color = type.GetParameterInteger(BuiltInParameter.LINE_COLOR);
  1182. string result = string.Format(",{0},{1},{2},{3},{4},{5},{6},{7},{8}", cxcd, cxkd, bzzg, zxjj, zdct, ycxc,
  1183. strDefault, lcct, color);
  1184. return result;
  1185. }
  1186. /// <summary>
  1187. /// 线创建标注
  1188. /// </summary>
  1189. /// <param name="doc"></param>
  1190. /// <param name="line"></param>
  1191. /// <param name="view"></param>
  1192. /// <param name="dType"></param>
  1193. /// <returns></returns>
  1194. public static Dimension CreateDimension(this Document doc, Line line, View view, DimensionType dType = null)
  1195. {
  1196. List<XYZ> ptList = new List<XYZ>();
  1197. ptList.Add(line.StartPoint());
  1198. ptList.Add(line.EndPoint());
  1199. Dimension dim = doc.CreateDimension(line, ptList, view);
  1200. if (dim != null && dType != null)
  1201. {
  1202. dim.DimensionType = dType;
  1203. }
  1204. return dim;
  1205. }
  1206. /// <summary>
  1207. /// 线上点创建标注
  1208. /// </summary>
  1209. /// <param name="doc"></param>
  1210. /// <param name="line"></param>
  1211. /// <param name="ptList"></param>
  1212. /// <param name="view"></param>
  1213. /// <param name="dType"></param>
  1214. /// <returns></returns>
  1215. public static Dimension CreateDimension(this Document doc, Line line, List<XYZ> ptList, View view,
  1216. DimensionType dType = null)
  1217. {
  1218. Dimension result = null;
  1219. ReferenceArray array = new ReferenceArray();
  1220. if (ptList != null && ptList.Count > 0)
  1221. {
  1222. //参照线方向
  1223. XYZ vector = line.Direction.VectorRotate(view.ViewDirection, Math.PI / 2);
  1224. foreach (XYZ item in ptList)
  1225. {
  1226. Reference refer = doc.GetReferenceDetail(item, vector, view);
  1227. if (refer != null)
  1228. {
  1229. array.Append(refer);
  1230. }
  1231. }
  1232. if (array.Size > 1)
  1233. {
  1234. result = doc.Create.NewDimension(view, line, array);
  1235. if (result != null && dType != null)
  1236. {
  1237. result.DimensionType = dType;
  1238. }
  1239. }
  1240. }
  1241. return result;
  1242. }
  1243. /// <summary>
  1244. /// 通过矩离创建标注
  1245. /// </summary>
  1246. /// <param name="doc"></param>
  1247. /// <param name="line"></param>
  1248. /// <param name="listSpace"></param>
  1249. /// <param name="view"></param>
  1250. /// <param name="dType"></param>
  1251. /// <returns></returns>
  1252. public static Dimension CreateDimension(this Document doc, Line line, List<double> listSpace, View view,
  1253. DimensionType dType = null)
  1254. {
  1255. double dStart = 0;
  1256. List<XYZ> listPt = new List<XYZ>();
  1257. for (int i = 0; i < listSpace.Count; i++)
  1258. {
  1259. double dSpace = listSpace[i];
  1260. listPt.Add(line.StartPoint() + line.Direction * dStart);
  1261. dStart += dSpace;
  1262. }
  1263. listPt.Add(line.StartPoint() + line.Direction * dStart);
  1264. return doc.CreateDimension(line, listPt, view, dType);
  1265. }
  1266. /// <summary>
  1267. /// 两道尺寸标注 测试用
  1268. /// </summary>
  1269. /// <param name="doc"></param>
  1270. /// <param name="line"></param>
  1271. /// <param name="listFirstSpace"></param>
  1272. /// <param name="listSecondSpace"></param>
  1273. /// <param name="view"></param>
  1274. /// <returns></returns>
  1275. public static List<Dimension> CreateDimension(this Document doc, Line line, List<double> listFirstSpace,
  1276. List<double> listSecondSpace, View view)
  1277. {
  1278. List<Dimension> listRtn = new List<Dimension>();
  1279. //参照线方向
  1280. XYZ vector = line.Direction.VectorRotate(view.ViewDirection, Math.PI / 2);
  1281. double dStart = 0;
  1282. List<XYZ> listPtFirst = new List<XYZ>();
  1283. for (int i = 0; i < listFirstSpace.Count; i++)
  1284. {
  1285. double dSpace = listFirstSpace[i].ToApi();
  1286. listPtFirst.Add(line.StartPoint() + line.Direction * dStart);
  1287. dStart += dSpace;
  1288. }
  1289. listPtFirst.Add(line.StartPoint() + line.Direction * dStart);
  1290. dStart = 0;
  1291. List<XYZ> listPtSecond = new List<XYZ>();
  1292. for (int i = 0; i < listSecondSpace.Count; i++)
  1293. {
  1294. double dSpace = listSecondSpace[i].ToApi();
  1295. listPtSecond.Add(line.StartPoint() + line.Direction * dStart);
  1296. dStart += dSpace;
  1297. }
  1298. listPtSecond.Add(line.StartPoint() + line.Direction * dStart);
  1299. List<XYZ> listPt = new List<XYZ>();
  1300. listPt.AddRange(listPtFirst);
  1301. foreach (var pt in listPtSecond)
  1302. {
  1303. if (!pt.ExsitsInlist(listPt))
  1304. {
  1305. listPt.Add(pt);
  1306. }
  1307. }
  1308. Dictionary<int, Reference> dicRef = new Dictionary<int, Reference>();
  1309. for (int i = 0; i < listPt.Count; i++)
  1310. {
  1311. XYZ item = listPt[i];
  1312. Reference refer = doc.GetReferenceDetail(item, vector, view);
  1313. if (refer != null)
  1314. {
  1315. dicRef.Add(i, refer);
  1316. }
  1317. }
  1318. ReferenceArray array = new ReferenceArray();
  1319. foreach (var pt in listPtFirst)
  1320. {
  1321. int index = listPt.FindIndex(p => p.IsEqual(pt));
  1322. array.Append(dicRef[index]);
  1323. }
  1324. if (array.Size > 1)
  1325. {
  1326. Dimension result = doc.Create.NewDimension(view, line, array);
  1327. listRtn.Add(result);
  1328. }
  1329. //两个字高
  1330. double dOffset = (3.0 * 2 * view.Scale).ToApi();
  1331. // line = line.Offset(dOffset);
  1332. array = new ReferenceArray();
  1333. foreach (var pt in listPtSecond)
  1334. {
  1335. int index = listPt.FindIndex(p => p.IsEqual(pt));
  1336. array.Append(dicRef[index]);
  1337. }
  1338. if (array.Size > 1)
  1339. {
  1340. Dimension result = doc.Create.NewDimension(view, line, array);
  1341. listRtn.Add(result);
  1342. }
  1343. return listRtn;
  1344. }
  1345. /// <summary>
  1346. /// 创建标注的短参照
  1347. /// </summary>
  1348. /// <param name="doc"></param>
  1349. /// <param name="start"></param>
  1350. /// <param name="vector"></param>
  1351. /// <param name="view"></param>
  1352. /// <returns></returns>
  1353. public static Reference GetReferenceDetail(this Document doc, XYZ start, XYZ vector, View view = null)
  1354. {
  1355. Reference result = null;
  1356. if (view == null)
  1357. {
  1358. view = doc.ActiveView;
  1359. }
  1360. Line line = vector.VectorToLine(start, 0.02 /*d.ToApi()*/);
  1361. if (view.ViewType == ViewType.ThreeD)
  1362. {
  1363. ModelCurve mc = LineExtend.NewModelCurve(line);
  1364. if (mc != null)
  1365. {
  1366. result = new Reference(mc);
  1367. }
  1368. }
  1369. else
  1370. {
  1371. DetailCurve dc = doc.Create.NewDetailCurve(view, line);
  1372. if (dc != null)
  1373. {
  1374. result = new Reference(dc);
  1375. }
  1376. }
  1377. return result;
  1378. }
  1379. /// <summary>
  1380. /// 获取标注配置
  1381. /// </summary>
  1382. /// <returns></returns>
  1383. private static string GetDimensionConfig()
  1384. {
  1385. //文件路径
  1386. string filePath = AppBaseInfo.AppTempFilePath + "\\ConfigDimensionType.dll";
  1387. string config = null;
  1388. #region 读取配置
  1389. if (File.Exists(filePath))
  1390. {
  1391. //存在本地文件 读取本地文件配置
  1392. using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read))
  1393. {
  1394. using (StreamReader sr = new StreamReader(fs, Encoding.Default))
  1395. {
  1396. config = sr.ReadLine();
  1397. sr.Close();
  1398. }
  1399. fs.Close();
  1400. }
  1401. }
  1402. #endregion
  1403. return config;
  1404. }
  1405. /// <summary>
  1406. /// 获取用户设置的标注样式
  1407. /// 该方法可设置尺寸界线长度
  1408. /// </summary>
  1409. /// <param name="doc"></param>
  1410. /// <param name="length"></param>
  1411. /// <returns></returns>
  1412. //public static DimensionType GetDimensionType(this Document doc, double? length = null, string config = null)
  1413. //{
  1414. // string defaultConfig = GetDimensionDefConfig();
  1415. // if (string.IsNullOrEmpty(config))
  1416. // config = GetDimensionConfig();
  1417. // bool isSave = true;
  1418. // if (length != null && length.Value > 0)
  1419. // {
  1420. // isSave = false;
  1421. // string[] array = config.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
  1422. // if (array.Length > 5)
  1423. // {
  1424. // array[5] = length.Value.ToString();
  1425. // config = ConvertToConfig(array);
  1426. // }
  1427. // array = defaultConfig.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
  1428. // if (array.Length > 5)
  1429. // {
  1430. // array[5] = length.Value.ToString();
  1431. // defaultConfig = ConvertToConfig(array);
  1432. // }
  1433. // }
  1434. // DimensionType result = CreateDimensionType(doc, config, isSave);
  1435. // if (result == null)
  1436. // result = CreateDimensionType(doc, defaultConfig, isSave);
  1437. // return result;
  1438. //}
  1439. private static string ConvertToConfig(string[] array)
  1440. {
  1441. string config = ",";
  1442. foreach (string item in array)
  1443. {
  1444. config += item + ",";
  1445. }
  1446. return config;
  1447. }
  1448. public static DimensionType FindDimensionType(this Document doc, string strTypeName)
  1449. {
  1450. DimensionType result = null;
  1451. List<DimensionType> dimTypes = doc.GetDimensionTypeSet();
  1452. foreach (DimensionType dimType in dimTypes)
  1453. {
  1454. /*
  1455. * 解决问题:在没有找到标注类型时,在不同模板中不能直接默认取第一个
  1456. * 应该取类型一致的
  1457. * DimensionStyleType弧长和角度标注有做的可能
  1458. */
  1459. if (result == null && dimType.StyleType == DimensionStyleType.Linear)
  1460. result = dimType;
  1461. if (!string.IsNullOrEmpty(dimType.Name)
  1462. && dimType.Name.Replace(" ", "").Trim().Equals(strTypeName.Replace(" ", "").Trim()))
  1463. {
  1464. return dimType;
  1465. }
  1466. }
  1467. return result;
  1468. }
  1469. public static bool CanMirrorElement(this Document doc, ElementId elemId)
  1470. {
  1471. return ElementTransformUtils.CanMirrorElement(doc, elemId);
  1472. }
  1473. public static bool CanMirrorElements(this Document doc, ICollection<ElementId> elemIds)
  1474. {
  1475. return ElementTransformUtils.CanMirrorElements(doc, elemIds);
  1476. }
  1477. public static ICollection<ElementId> CopyElement(this Document doc, ElementId elem, XYZ translation)
  1478. {
  1479. return ElementTransformUtils.CopyElement(doc, elem, translation);
  1480. }
  1481. public static ICollection<ElementId> CopyElements(this Document doc, ICollection<ElementId> elementsToCopy,
  1482. XYZ translation)
  1483. {
  1484. return ElementTransformUtils.CopyElements(doc, elementsToCopy, translation);
  1485. }
  1486. public static void MirrorElement(this Document doc, ElementId elementToMirror, Plane plane)
  1487. {
  1488. ElementTransformUtils.MirrorElement(doc, elementToMirror, plane);
  1489. }
  1490. public static void MirrorElements(this Document doc, ICollection<ElementId> elementsToMirror, Plane plane)
  1491. {
  1492. ElementTransformUtils.MirrorElements(doc, elementsToMirror, plane, true);
  1493. }
  1494. public static void MoveElement(this Document doc, ElementId elementToMove, XYZ translation)
  1495. {
  1496. ElementTransformUtils.MoveElement(doc, elementToMove, translation);
  1497. }
  1498. public static void MoveElements(this Document doc, ICollection<ElementId> elem, XYZ translation)
  1499. {
  1500. ElementTransformUtils.MoveElements(doc, elem, translation);
  1501. }
  1502. public static void RotateElement(this Document doc, Element elem, XYZ pt, double angle)
  1503. {
  1504. Line axis = Line.CreateBound(pt, pt.AddZ(1));
  1505. doc.RotateElement(elem, axis, angle);
  1506. }
  1507. public static void RotateElement(this Document doc, Element elem, Line axis, double angle)
  1508. {
  1509. ElementTransformUtils.RotateElement(doc, elem.Id, axis, angle);
  1510. }
  1511. public static void RotateElement(this Document doc, ElementId elem, Line axis, double angle)
  1512. {
  1513. ElementTransformUtils.RotateElement(doc, elem, axis, angle);
  1514. }
  1515. public static void RotateElements(this Document doc, ICollection<ElementId> elementsToRotate, Line axis,
  1516. double angle)
  1517. {
  1518. ElementTransformUtils.RotateElements(doc, elementsToRotate, axis, angle);
  1519. }
  1520. public static void RotateElements(this Document doc, ICollection<Element> elementsToRotate, Line axis,
  1521. double angle)
  1522. {
  1523. List<ElementId> elementIdsToRotate = new List<ElementId>();
  1524. foreach (Element e in elementsToRotate)
  1525. {
  1526. elementIdsToRotate.Add(e.Id);
  1527. }
  1528. ElementTransformUtils.RotateElements(doc, elementIdsToRotate, axis, angle);
  1529. }
  1530. /// <summary>
  1531. /// 创建明细表
  1532. /// </summary>
  1533. /// <param name="viewScheduleName">明细表名称</param>
  1534. /// <param name="filedArray">明细表字段名集合</param>
  1535. ///<param name="isSharedParam">是否取共享参数</param>
  1536. /// <returns></returns>
  1537. public static ViewSchedule CreateViewSchedule(this Document doc, string viewScheduleName,
  1538. BuiltInCategory category, string[] filedArray, ElementId areaSchemaId = null)
  1539. {
  1540. List<ViewSchedule> scheduleList = doc.GetElements(typeof(ViewSchedule)).ToList<ViewSchedule>();
  1541. ViewSchedule schedule = scheduleList.Find(p => p.Name == viewScheduleName);
  1542. if (schedule != null)
  1543. {
  1544. return schedule;
  1545. }
  1546. Transaction trans = new Transaction(doc, "CreateViewSchedule");
  1547. trans.Start();
  1548. try
  1549. {
  1550. if (areaSchemaId == null)
  1551. schedule = ViewSchedule.CreateSchedule(doc, new ElementId(category), ElementId.InvalidElementId);
  1552. else
  1553. schedule = ViewSchedule.CreateSchedule(doc, new ElementId(category), areaSchemaId);
  1554. #region 创建明细表表头
  1555. TableData tableData = schedule.GetTableData();
  1556. if (tableData != null)
  1557. {
  1558. TableSectionData sectionData = tableData.GetSectionData(0);
  1559. if (sectionData != null && sectionData.NumberOfColumns == 0 && sectionData.NumberOfRows == 0)
  1560. {
  1561. //#if R16
  1562. sectionData.InsertColumn(0);
  1563. //#else
  1564. //sectionData.InsertColumn(0, true);
  1565. //#endif
  1566. sectionData.SetColumnWidth(0, tableData.Width);
  1567. doc.Regenerate();
  1568. sectionData.InsertRow(0);
  1569. sectionData.SetCellText(0, 0, viewScheduleName);
  1570. }
  1571. }
  1572. #endregion
  1573. schedule.Name = viewScheduleName;
  1574. ScheduleDefinition definition = schedule.Definition;
  1575. definition.ShowGrandTotal = true;
  1576. definition.ShowGrandTotalTitle = true;
  1577. List<SchedulableField> filedList = schedule.Definition.GetSchedulableFields().ToList();
  1578. foreach (string s in filedArray)
  1579. {
  1580. SchedulableField filed = filedList.Find(p => p.GetName(doc) == s);
  1581. if (filed != null)
  1582. {
  1583. ScheduleField scheduleField = schedule.Definition.AddField(filed);
  1584. if (s.Contains("SAGA")) scheduleField.ColumnHeading = s.Substring(3);
  1585. if (scheduleField.CanTotal())
  1586. {
  1587. scheduleField.SetHasTotals();
  1588. }
  1589. }
  1590. }
  1591. trans.Commit();
  1592. return schedule;
  1593. }
  1594. catch
  1595. {
  1596. trans.RollBack();
  1597. }
  1598. return null;
  1599. }
  1600. /// <summary>
  1601. /// 删除扩展
  1602. /// </summary>
  1603. /// <param name="doc"></param>
  1604. /// <param name="element"></param>
  1605. public static void DeleteExt(this Document doc, Element element)
  1606. {
  1607. //修改原因:Delete(element.Id)不能删除其上的子对象
  1608. element.Delete();
  1609. }
  1610. /// <summary>
  1611. /// 计算文本标签的宽度
  1612. /// (需在事物中调用)
  1613. /// </summary>
  1614. /// <param name="doc"></param>
  1615. /// <param name="view"></param>
  1616. /// <param name="str"></param>
  1617. /// <param name="type"></param>
  1618. /// <returns></returns>
  1619. // public static double ComputeTextNoteWidth(this Document doc, View view, string str,
  1620. // TextNoteType type, double singleWidth = 0)
  1621. // {
  1622. // double d1 = 0;
  1623. // if (singleWidth == 0)
  1624. // singleWidth = doc.ComputeSingleWidth(view, str, type);
  1625. // d1 = singleWidth;
  1626. // if (str.Contains('\n'))
  1627. // {
  1628. // List<string> list = new List<string>();
  1629. // list.AddRange(str.Split('\n'));
  1630. // str = list.OrderByDescending(s => s.Length).First();
  1631. // }
  1632. // //一个汉字占两个字符位
  1633. // int k = Encoding.Default.GetByteCount(str);
  1634. // //暂时未计算探索者钢筋符号的信息,探索者钢筋符号中一个符号占一个字符位,但是大小为两个字符位的大小
  1635. // //如果包含空格,每多一个空格,增加一个字符宽度
  1636. // char[] charArray = str.ToArray();
  1637. // int count = 0;
  1638. // foreach (char item in charArray)
  1639. // {
  1640. // if (string.IsNullOrEmpty(item.ToString().Trim()))
  1641. // {
  1642. // count++;
  1643. // }
  1644. // }
  1645. //#if R15
  1646. // count++;//15中的字符末尾会多出一个空格
  1647. // k++;
  1648. //#endif
  1649. // return d1*(k + count);
  1650. // }
  1651. ///// <summary>
  1652. ///// 创建标注
  1653. ///// </summary>
  1654. ///// <param name="doc"></param>
  1655. ///// <param name="list">所有详图线</param>
  1656. ///// <param name="start">标注起点(转换前坐标)</param>
  1657. ///// <param name="end">标注终点(转换前坐标)</param>
  1658. ///// <param name="dimPoint">标注点(转换前坐标)</param>
  1659. ///// <param name="drawView">视图</param>
  1660. //public static Dimension CreateDimension(this Document doc, List<DetailCurve> list, XYZ start, XYZ end,
  1661. // XYZ dimPoint, View drawView, string showText = null, DimensionType type = null)
  1662. //{
  1663. // Line line = Line.CreateBound(start, end);
  1664. // XYZ foot = line.VerticalPoint(dimPoint);
  1665. // XYZ vector = dimPoint.Subtract(foot).Normalize();
  1666. // if (vector.IsEqual(XYZ.Zero))
  1667. // {
  1668. // vector = line.GetVUnboundLine(start).LineToBound().UnitVector().Normalize();
  1669. // }
  1670. // line = line.OffsetPoint(dimPoint);
  1671. // Reference startRef = start.GetReferenceByDetailList(list, vector, drawView);
  1672. // Reference endRef = end.GetReferenceByDetailList(list, vector, drawView);
  1673. // if (startRef != null && endRef != null)
  1674. // {
  1675. // ReferenceArray array = new ReferenceArray();
  1676. // array.Append(startRef);
  1677. // array.Append(endRef);
  1678. // Dimension dim = null;
  1679. // if (type != null)
  1680. // dim = doc.Create.NewDimension(drawView, line, array, type);
  1681. // else
  1682. // dim = doc.Create.NewDimension(drawView, line, array);
  1683. // if (dim != null)
  1684. // {
  1685. // if (showText != null && !showText.Equals("<>"))
  1686. // dim.ValueOverride = showText;
  1687. // }
  1688. // return dim;
  1689. // }
  1690. // return null;
  1691. //}
  1692. //public static Reference GetReferenceByDetailList(this XYZ point, List<DetailCurve> detailList, XYZ vector,
  1693. // View drawView)
  1694. //{
  1695. // if (detailList == null)
  1696. // detailList = new List<DetailCurve>();
  1697. // Reference refLine = null;
  1698. // XYZ end = point + vector*0.015 /*d.ToApi()*/;
  1699. // Line line = Line.CreateBound(point, end);
  1700. // DetailCurve dc = detailList.Find(p => p.GeometryCurve.IsOnCurve(line));
  1701. // if (dc != null)
  1702. // refLine = new Reference(dc);
  1703. // else //没有的补一根线
  1704. // {
  1705. // CurveArray ca = new CurveArray();
  1706. // ca.Append(line);
  1707. // List<DetailCurve> list = ca.DrawDetailLines(drawView);
  1708. // if (list != null && list.Count > 0)
  1709. // refLine = new Reference(list[0]);
  1710. // }
  1711. // return refLine;
  1712. //}
  1713. /// <summary>
  1714. /// 获取一个标注的所有参照
  1715. /// (提出该方法理由:从标注中直接取出的参照在16版本不能直接使用)
  1716. /// </summary>
  1717. /// <param name="doc"></param>
  1718. /// <param name="dim"></param>
  1719. /// <returns></returns>
  1720. public static ReferenceArray GetReferences(this Dimension dim)
  1721. {
  1722. #if R16
  1723. ReferenceArray array = new ReferenceArray();
  1724. foreach (Reference item in dim.References)
  1725. {
  1726. Element elem = ExternalDataWrapper.Current.Doc.GetElement(item);
  1727. if (elem != null)
  1728. array.Append(new Reference(elem));
  1729. }
  1730. #else
  1731. ReferenceArray array = dim.References;
  1732. #endif
  1733. return array;
  1734. }
  1735. public static List<SlabEdgeType> GetSlabEdgeTypesEx(this Document doc)
  1736. {
  1737. FilteredElementCollector collector = new FilteredElementCollector(doc);
  1738. collector.OfClass(typeof(SlabEdgeType));
  1739. return collector.OfType<SlabEdgeType>().ToList();
  1740. }
  1741. public static List<RoomTagType> RoomTagTypesEx(this Document doc)
  1742. {
  1743. FilteredElementCollector collector = new FilteredElementCollector(doc);
  1744. collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_RoomTags);
  1745. return collector.OfType<RoomTagType>().ToList();
  1746. }
  1747. public static List<T> GetElementsT<T>(this Document doc, IEnumerable<ElementId> elementIds) where T : Element
  1748. {
  1749. List<T> listElements = new List<T>();
  1750. if (elementIds == null)
  1751. return listElements;
  1752. foreach (ElementId elementId in elementIds)
  1753. {
  1754. T tv = doc.GetElementT<T>(elementId);
  1755. if (tv != null)
  1756. listElements.Add(tv);
  1757. }
  1758. return listElements;
  1759. }
  1760. }
  1761. public class SAGAFamilyLoadOptions : IFamilyLoadOptions
  1762. {
  1763. public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
  1764. {
  1765. overwriteParameterValues = true;
  1766. return true;
  1767. }
  1768. public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source,
  1769. out bool overwriteParameterValues)
  1770. {
  1771. source = FamilySource.Family;
  1772. overwriteParameterValues = true;
  1773. return true;
  1774. }
  1775. }
  1776. public enum DrivenTypes
  1777. {
  1778. NotSupport = 0,
  1779. Point,
  1780. Curve,
  1781. Face
  1782. }
  1783. }