123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.Architecture;
- using Autodesk.Revit.DB.Structure;
- using Autodesk.Revit.UI.Selection;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Extend;
- using SAGA.RevitAPI;
- using SAGA.RevitUtils.MEP;
- namespace SAGA.RevitUtils.Extends
- {
- public static class DocumentExtend
- {
- /// <summary>
- /// rvt项目名称
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static string GetShortTitle(this Document doc)
- {
- string strTitle = doc.Title;
- string strExtend = Path.GetExtension(strTitle);
- if (strExtend != null && strExtend.Length > 0)
- {
- if (!strExtend.StartsWith("."))
- strExtend = "." + strExtend;
- int intIndex = strTitle.IndexOf(strExtend);
- if (intIndex > 0)
- strTitle = strTitle.Substring(0, intIndex);
- }
- if (strTitle.Length > 12 + 3)
- {
- if (strExtend != null && strExtend.Length > 0)
- {
- return strTitle.Substring(0, 12) + "..." + strExtend;
- }
- return strTitle.Substring(0, 12) + "...";
- }
- return doc.Title;
- }
- /// <summary>
- /// 文档信息
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static string GetPathNameOrTitle(this Document doc)
- {
- //新建时为空,返回项目名称
- if (string.IsNullOrEmpty(doc.PathName))
- return doc.Title;
- //项目磁盘全文件名
- return doc.PathName;
- }
- /// <summary>
- /// 文档所有图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="isOnlyInstance">默认只取实例图元</param>
- /// <returns></returns>
- public static List<Element> GetAllElements(this Document doc, bool isContainElementType = false)
- {
- if (isContainElementType)
- {
- FilteredElementCollector elemTypeCtor = (new FilteredElementCollector(doc)).WhereElementIsElementType();
- FilteredElementCollector notElemTypeCtor =
- (new FilteredElementCollector(doc)).WhereElementIsNotElementType();
- FilteredElementCollector allElementCtor = elemTypeCtor.UnionWith(notElemTypeCtor);
- return allElementCtor.ToElements().ToList<Element>();
- }
- else
- {
- FilteredElementCollector notElemTypeCtor =
- (new FilteredElementCollector(doc)).WhereElementIsNotElementType();
- return notElemTypeCtor.ToElements().ToList<Element>();
- }
- }
- /// <summary>
- /// 大部分几何图元,有一部分没用的
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static List<Element> GetGeomElements(this Document doc)
- {
- double dExtend = 1000000;
- dExtend = dExtend.ToApi();
- XYZ ptMin = new XYZ(-dExtend, -dExtend, -dExtend);
- XYZ ptMax = new XYZ(dExtend, dExtend, dExtend);
- Outline otl = new Outline(ptMin, ptMax);
- return doc.GetElements(otl);
- }
- /// <summary>
- /// 空间矩形框
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="baseLevel"></param>
- /// <param name="topLevel"></param>
- /// <param name="baseOffset"></param>
- /// <param name="topOffset"></param>
- /// <returns></returns>
- public static Outline CreateOutline(this Document doc, Level baseLevel, Level topLevel, double baseOffset,
- double topOffset)
- {
- if (baseLevel.IsEqual(topLevel))
- {
- List<Level> listLevel = doc.GetLevels();
- int intIndex = listLevel.FindIndex(p => p.IsEqual(baseLevel));
- if (intIndex == 0)
- {
- topLevel = listLevel[intIndex + 1];
- }
- if (intIndex > 0)
- {
- baseLevel = listLevel[intIndex - 1];
- }
- }
- double dExtend = 9999999999d.ToApi();
- baseOffset = baseOffset.ToApi();
- topOffset = topOffset.ToApi();
- XYZ ptMin = new XYZ(-dExtend, -dExtend, baseLevel.Elevation + baseOffset);
- XYZ ptMax = new XYZ(dExtend, dExtend, topLevel.Elevation + topOffset);
- Outline otl = new Outline(ptMin, ptMax);
- return otl;
- }
- public static ElementFilter CreateAvailablyFilter(this Document doc)
- {
- if (true)
- {
- LogicalAndFilter andFilter = new LogicalAndFilter(new List<ElementFilter>
- {
- new ElementCategoryFilter(BuiltInCategory.OST_Elev, true),
- new ElementCategoryFilter(BuiltInCategory.OST_Cameras, true),
- new ElementCategoryFilter(BuiltInCategory.OST_Views, true),
- new ElementCategoryFilter(BuiltInCategory.OST_Viewers, true),
- new ElementCategoryFilter(BuiltInCategory.OST_Grids, true),
- new ElementCategoryFilter(BuiltInCategory.OST_SharedBasePoint, true),
- new ElementCategoryFilter(BuiltInCategory.OST_ProjectBasePoint, true),
- new ElementCategoryFilter(BuiltInCategory.OST_AnalyticalNodes, true),
- //new ElementCategoryFilter(BuiltInCategory.OST_WallAnalytical, true),
- });
- return andFilter;
- }
- else
- {
- LogicalOrFilter orFilter = new LogicalOrFilter(new List<ElementFilter>
- {
- new ElementCategoryFilter(BuiltInCategory.OST_StructConnections),
- new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionTags),
- //new ElementCategoryFilter(BuiltInCategory.OST_StructLocationLineControl),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralAnnotations),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralBracePlanReps),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumnTags),
- //new ElementCategoryFilter(BuiltInCategory.OST_StructuralFoundation),
- //new ElementCategoryFilter(BuiltInCategory.OST_StructuralFoundationTags),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingOpening),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingOther),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingSystem),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingTags),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralStiffener),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralStiffenerTags),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralTruss),
- new ElementCategoryFilter(BuiltInCategory.OST_Floors),
- new ElementCategoryFilter(BuiltInCategory.OST_FloorOpening),
- new ElementCategoryFilter(BuiltInCategory.OST_FloorTags),
- new ElementCategoryFilter(BuiltInCategory.OST_Walls),
- new ElementCategoryFilter(BuiltInCategory.OST_WallTags),
- new ElementCategoryFilter(BuiltInCategory.OST_GenericModel),
- new ElementCategoryFilter(BuiltInCategory.OST_GenericModel),
- });
- return orFilter;
- }
- }
- /// <summary>
- /// 两个标高之间和图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="levelBase"></param>
- /// <param name="levelTop"></param>
- /// <param name="baseOffset"></param>
- /// <param name="topOffset"></param>
- /// <param name="viewDisciplineT"></param>
- /// <returns></returns>
- public static List<Element> GetAvailablyElements(this Document doc, Level levelBase, Level levelTop,
- double baseOffset, double topOffset, ViewDisciplineT viewDisciplineT)
- {
- Outline otl = doc.CreateOutline(levelBase, levelTop, baseOffset, topOffset);
- ElementFilter elementeFilter = doc.CreateAvailablyFilter(viewDisciplineT);
- return doc.GetElements(otl, elementeFilter);
- }
- public static ElementFilter CreateAvailablyFilter(this Document doc, ViewDisciplineT viewDisciplineT)
- {
- if (viewDisciplineT == ViewDisciplineT.Architectural)
- {
- LogicalAndFilter andFilter = new LogicalAndFilter(new List<ElementFilter>
- {
- new ElementCategoryFilter(BuiltInCategory.OST_Elev, true),
- new ElementCategoryFilter(BuiltInCategory.OST_Cameras, true),
- new ElementCategoryFilter(BuiltInCategory.OST_Views, true),
- new ElementCategoryFilter(BuiltInCategory.OST_Viewers, true),
- new ElementCategoryFilter(BuiltInCategory.OST_Grids, true),
- new ElementCategoryFilter(BuiltInCategory.OST_SharedBasePoint, true),
- new ElementCategoryFilter(BuiltInCategory.OST_ProjectBasePoint, true),
- new ElementCategoryFilter(BuiltInCategory.OST_AnalyticalNodes, true),
- //new ElementCategoryFilter(BuiltInCategory.OST_WallAnalytical, true),
- });
- return andFilter;
- }
- else
- {
- LogicalOrFilter orFilter = new LogicalOrFilter(new List<ElementFilter>
- {
- new ElementCategoryFilter(BuiltInCategory.OST_StructConnections),
- new ElementCategoryFilter(BuiltInCategory.OST_StructConnectionTags),
- //new ElementCategoryFilter(BuiltInCategory.OST_StructLocationLineControl),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralAnnotations),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralBracePlanReps),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumnTags),
- //new ElementCategoryFilter(BuiltInCategory.OST_StructuralFoundation),
- //new ElementCategoryFilter(BuiltInCategory.OST_StructuralFoundationTags),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingOpening),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingOther),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingSystem),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralFramingTags),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralStiffener),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralStiffenerTags),
- new ElementCategoryFilter(BuiltInCategory.OST_StructuralTruss),
- new ElementCategoryFilter(BuiltInCategory.OST_Floors),
- new ElementCategoryFilter(BuiltInCategory.OST_FloorOpening),
- new ElementCategoryFilter(BuiltInCategory.OST_FloorTags),
- new ElementCategoryFilter(BuiltInCategory.OST_Walls),
- new ElementCategoryFilter(BuiltInCategory.OST_WallTags),
- new ElementCategoryFilter(BuiltInCategory.OST_GenericModel),
- new ElementCategoryFilter(BuiltInCategory.OST_GenericModel),
- });
- return orFilter;
- }
- }
- public static List<Element> GetAvailablyElements(this Document doc, Level levelBase, Level levelTop,
- double baseOffset, double topOffset, ViewDisciplineT viewDisciplineT, out List<Element> allElements)
- {
- allElements = null;
- Outline otl = doc.CreateOutline(levelBase, levelTop, baseOffset, topOffset);
- ElementFilter elementeFilter = doc.CreateAvailablyFilter(viewDisciplineT);
- allElements = doc.GetElements(otl);
- return doc.GetElements(otl, elementeFilter);
- }
- public static double GetWallSweepOrSteps(this Document doc, Wall wall, out double h)
- {
- h = 0;
- List<WallSweep> wallSweepFilter = doc.FilterElements<WallSweep>();
- List<FamilyInstance> instanceFilter =
- doc.FilterElements<FamilyInstance>().Where(p => p.Symbol.Name.Contains("坡道")).ToList();
- if (wallSweepFilter.Count > 0)
- {
- foreach (WallSweep wallSweep in wallSweepFilter)
- {
- List<ElementId> ids = wallSweep.GetHostIds().ToList();
- foreach (ElementId id in ids)
- {
- if (id.IntegerValue == wall.Id.IntegerValue)
- {
- double heigth = wallSweep.GetParameterDoubleMm(BuiltInParameter.WALL_SWEEP_OFFSET_PARAM);
- //foreach (FamilyInstance instance in instanceFilter)
- //{
- //if (instance.Host.Id.IntegerValue == wall.Id.IntegerValue)
- //{
- if (heigth == 0)
- h = heigth;
- else
- h = heigth - 0.5;
- //}
- //}
- }
- }
- }
- }
- return h;
- }
- public static double GetWallSweepOrStairs(this Document doc, Wall wall, out double h)
- {
- h = 0;
- List<WallSweep> wallSweepFilter = doc.FilterElements<WallSweep>();
- List<FamilyInstance> instanceFilter =
- doc.FilterElements<FamilyInstance>().Where(p => p.Symbol.Family.Name.Contains("台阶")).ToList();
- if (wallSweepFilter.Count > 0)
- {
- foreach (WallSweep wallSweep in wallSweepFilter)
- {
- List<ElementId> ids = wallSweep.GetHostIds().ToList();
- foreach (ElementId id in ids)
- {
- if (id.IntegerValue == wall.Id.IntegerValue)
- {
- double heigth = wallSweep.GetParameterDoubleMm(BuiltInParameter.WALL_SWEEP_OFFSET_PARAM);
- //foreach (FamilyInstance instance in instanceFilter)
- //{
- //if (instance.Host.Id.IntegerValue == wall.Id.IntegerValue)
- //{
- if (heigth == 0)
- h = heigth;
- else
- h = heigth - 0.5;
- //}
- //}
- }
- }
- }
- }
- return h;
- }
- public static List<Element> GetWallOrBeams(this Document doc)
- {
- ElementClassFilter wallFilter = new ElementClassFilter(typeof(Wall));
- ElementClassFilter instanceFilter = new ElementClassFilter(typeof(FamilyInstance));
- ElementStructuralTypeFilter structuralTypeFilter = new ElementStructuralTypeFilter(StructuralType.Beam);
- LogicalAndFilter beamFilter = new LogicalAndFilter(instanceFilter, structuralTypeFilter);
- LogicalOrFilter wallOrBeam = new LogicalOrFilter(wallFilter, beamFilter);
- return doc.GetElements(doc.ActiveView, wallOrBeam);
- }
- /// <summary>
- /// 分类
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="category"></param>
- /// <returns></returns>
- public static Category GetCategory(this Document doc, BuiltInCategory category)
- {
- try
- {
- //有报错,如OST_StairsStringerCarriage 2015-10-26
- return doc.Settings.Categories.get_Item(category);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return null;
- }
- /// <summary>
- /// 获取某一类别的重叠元素集(注:主要针对梁柱墙板构件)
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="category"></param>
- /// <returns></returns>
- public static List<List<Element>> GetOverlapElements(this Document doc, BuiltInCategory category)
- {
- List<List<Element>> listElementList = new List<List<Element>>();
- List<Element> elementList = new List<Element>();
- elementList = doc.FilterElements(category);
- //建筑柱和建筑柱重叠需要检查,建筑柱和结构柱重叠,不算重叠。注:mjy 修改BUG
- foreach (Element element in elementList)
- {
- List<Element> resultList = new List<Element>();
- switch (category)
- {
- case BuiltInCategory.OST_Columns:
- case BuiltInCategory.OST_StructuralColumns:
- FamilyInstance column = element as FamilyInstance;
- //double height = column.GetParameterDouble("柱截面高");
- //double baseOffset = column.GetParameterDouble(BuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM);
- //double topOffset = column.GetParameterDouble(BuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM);
- //XYZ point = (element as FamilyInstance).GetLocationPoint();
- //XYZ basePoint = point.NewZ(point.Z + baseOffset);
- //XYZ topPoint = point.NewZ(point.Z + baseOffset + height + topOffset);
- //Outline outline = new Outline(basePoint, topPoint);
- //BoundingBoxIntersectsFilter filter = new BoundingBoxIntersectsFilter(outline);
- //ElementCategoryFilter categoryFilter = new ElementCategoryFilter(category);
- //LogicalAndFilter andFiter = new LogicalAndFilter(filter, categoryFilter);
- //resultList = doc.GetElements(andFiter);
- ElementCategoryFilter categoryFilter = new ElementCategoryFilter(category);
- resultList = doc.GetElements(column, categoryFilter, 10d.ToApi());
- break;
- case BuiltInCategory.OST_StructuralFraming:
- Curve curve = (element as FamilyInstance).GetLocationCurve();
- XYZ startPoint = curve.StartPoint();
- XYZ endPoint = curve.EndPoint();
- XYZ norm = endPoint.Subtract(startPoint).Normalize();
- startPoint += 0.01 * norm;
- endPoint -= 0.01 * norm;
- List<Element> leftElements = doc.GetElements(startPoint);
- List<Element> rightElements = doc.GetElements(endPoint);
- leftElements.RemoveAll(p => p.Id.IsEqual(element.Id) || !p.Category.IsEqual(category));
- rightElements.RemoveAll(p => p.Id.IsEqual(element.Id) || !p.Category.IsEqual(category));
- resultList = leftElements.Concat(rightElements).ToList();
- break;
- default:
- resultList = element.GetIntersectElements(category, true);
- break;
- }
- #region 对墙进行二次处理 只保留定位线平行的墙
- if (category == BuiltInCategory.OST_Walls)
- {
- Wall wall = element as Wall;
- resultList.RemoveAll(w => !((Wall)w).Location.GetCurve().IsParallel(wall.Location.GetCurve()));
- }
- if (category.Equals(BuiltInCategory.OST_StructuralFraming))
- {
- FamilyInstance beam = element as FamilyInstance;
- Curve beamCurve = beam.GetLocationCurve();
- resultList.RemoveAll(p => !((FamilyInstance)p).GetLocationCurve().IsParallel(beamCurve));
- }
- #endregion
- if (resultList.Count <= 1) continue;
- #region 循环判断resultList的元素是否存在于listElementList,若存在则添加resultList中元素到对应序列中,若不存在则把resultList作为新项添加到listElementList中
- if (listElementList.Exists(p => p.Exists(q => resultList.Exists(r => r.IsEqual(q)))))
- {
- foreach (List<Element> elList in listElementList)
- {
- if (resultList.Exists(p => elList.Exists(q => q.IsEqual(p))))
- {
- elList.AddRange(resultList.FindAll(p => !elList.Exists(q => p.IsEqual(q))).ToArray());
- break;
- }
- }
- }
- else listElementList.Add(resultList);
- #endregion
- }
- return listElementList;
- }
- public static DisplayUnitType GetDisplayUnitType(this Document doc)
- {
- try
- {
- UnitType unittype = UnitType.UT_Length;
- Units projectUnit = doc.GetUnits();
- FormatOptions formatOption = projectUnit.GetFormatOptions(unittype);
- return formatOption.DisplayUnits;
- }
- catch
- {
- return DisplayUnitType.DUT_DECIMAL_FEET;
- }
- }
- /// <summary>
- /// 过滤类型对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="view"></param>
- /// <returns></returns>
- public static List<T> FilterElements<T>(this Document doc, View view = null) where T : class
- {
- FilteredElementCollector collector = null;
- if (view == null)
- {
- collector = new FilteredElementCollector(doc);
- }
- else
- {
- collector = new FilteredElementCollector(doc, view.Id);
- }
- return collector.OfClass(typeof(T)).ToList<T>();
- }
- /// <summary>
- /// 过滤类型对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="condition"></param>
- /// <returns></returns>
- public static List<T> FilterElements<T>(this Document doc, Predicate<T> condition) where T : Element
- {
- List<T> listItems = doc.FilterElements<T>();
- List<T> listRtn = new List<T>();
- foreach (T item in listItems)
- {
- if (condition(item))
- listRtn.Add(item);
- }
- return listRtn;
- }
- /// <summary>
- /// 过滤类型对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static List<T> FilterElements<T>(this Document doc, ElementFilter filter) where T : Element
- {
- var collector = new FilteredElementCollector(doc);
- //collector = collector.OfClass(typeof(T));
- collector = collector.WherePasses(filter);
- return collector.ToList<T>();
- }
- /// <summary>
- /// 类型过滤图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public static List<Element> FilterElements(this Document doc, Type type)
- {
- var collector = new FilteredElementCollector(doc);
- return collector.OfClass(type).ToList<Element>();
- }
- /// <summary>
- /// 过滤器过滤图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static List<Element> FilterElements(this Document doc, ElementFilter filter)
- {
- var collector = new FilteredElementCollector(doc);
- return collector.WherePasses(filter).ToList<Element>();
- }
- /// <summary>
- /// 过滤器过滤图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static List<Element> FilterElements(this Document doc, IEnumerable<Element> elements, ElementFilter filter)
- {
- var collector = new FilteredElementCollector(doc, elements.Select(t => t.Id).ToList());
- return collector.WherePasses(filter).ToList<Element>();
- }
- /// <summary>
- /// 分类过滤实例图元
- ///</summary>
- /// <param name="doc"></param>
- /// <param name="bic"></param>
- /// <returns></returns>
- public static List<Element> FilterElements(this Document doc, BuiltInCategory bic)
- {
- List<Element> listRtn = null;
- var filter = new ElementCategoryFilter(bic);
- var collector = new FilteredElementCollector(doc);
- listRtn = collector.WherePasses(filter).WhereElementIsNotElementType().ToList<Element>();
- return listRtn;
- }
- /// <summary>
- /// 分类过滤类型图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="bic"></param>
- /// <returns></returns>
- public static List<ElementType> FilterElementTypes(this Document doc, BuiltInCategory bic)
- {
- List<ElementType> listRtn = null;
- var filter = new ElementCategoryFilter(bic);
- var collector = new FilteredElementCollector(doc);
- listRtn = collector.WherePasses(filter).WhereElementIsElementType().ToList<ElementType>();
- return listRtn;
- }
- /// <summary>
- /// 获取类型
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="bic"></param>
- /// <returns></returns>
- public static List<T> FilterElementTypes<T>(this Document doc, BuiltInCategory bic)
- {
- List<T> listRtn = null;
- var filter = new ElementCategoryFilter(bic);
- var collector = new FilteredElementCollector(doc);
- listRtn = collector.WherePasses(filter).WhereElementIsElementType().ToList<T>();
- return listRtn;
- }
- public static List<FamilyInstance> FilterInstances(this Document doc, IAllowElement allowElement,
- DrivenTypes drivenType)
- {
- List<FamilyInstance> items = null;
- var collector = new FilteredElementCollector(doc);
- switch (drivenType)
- {
- case DrivenTypes.Curve:
- items = collector.WherePasses(new ElementIsCurveDrivenFilter(false))
- .OfClass(typeof(FamilyInstance)).ToList<FamilyInstance>();
- break;
- case DrivenTypes.Point:
- default:
- break;
- }
- if (items != null) return items.Where(allowElement.IsAllow).ToList();
- return null;
- }
- /// <summary>
- /// id返回图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="intId"></param>
- /// <returns></returns>
- public static Element GetElement(this Document doc, int intId)
- {
- try
- {
- var id = new ElementId(intId);
- return doc.GetElement(id);
- }
- catch (Exception e)
- {
-
- return null;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="id"></param>
- /// <returns></returns>
- public static T GetElementT<T>(this Document doc, ElementId id) where T : Element
- {
- return doc.GetElement(id) as T;
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- public static T GetElementT<T>(this Document doc, string name) where T : Element
- {
- return doc.GetElement(name) as T;
- }
- /// <summary>
- /// 获取图元
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static List<T> GetElements<T>(this Document doc) where T : Element
- {
- return doc.FilterElements<T>();
- }
- /// <summary>
- /// 根据名称判断某类图元是否存在 2015-11-25
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="strName"></param>
- /// <returns></returns>
- public static bool IsExistElement<T>(this Document doc, string strName) where T : Element
- {
- return doc.GetElements<T>().Exists(p => p.Name == strName);
- }
- /// <summary>
- /// 分类图元
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="bic"></param>
- /// <returns></returns>
- public static List<T> GetElements<T>(this Document doc, BuiltInCategory bic) where T : Element
- {
- var filter = new ElementCategoryFilter(bic);
- return doc.FilterElements<T>(filter);
- }
- /// <summary>
- /// 相交的图元,慢过滤
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="elem"></param>
- /// <returns></returns>
- public static List<T> GetElements<T>(this Document doc, Element elem) where T : Element
- {
- var collector = new FilteredElementCollector(doc);
- var filter = new ElementIntersectsElementFilter(elem);
- return collector.WherePasses(filter).ToList<T>();
- }
- /// <summary>
- /// 获取图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, Type type)
- {
- return doc.FilterElements(type);
- }
- /// <summary>
- /// 通过类型获取图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="eleType"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, ElementType eleType)
- {
- List<Element> listRtn;
- BuiltInCategory cat = eleType.GetCategory();
- //不是所有的图元都有Category
- if (cat != BuiltInCategory.INVALID)
- {
- listRtn = doc.FilterElements(cat);
- }
- else
- {
- listRtn =
- doc.GetAllElements(true).FindAll(p => p.GetElementType() != null && p.GetElementType().IsEqual(eleType));
- }
- return listRtn;
- }
- /// <summary>
- /// 包含某一参数的图元
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="doc"></param>
- /// <param name="para"></param>
- /// <returns></returns>
- public static List<T> GetElements<T>(this Document doc, BuiltInParameter para) where T : Element
- {
- return doc.GetElements<T>().FindAll(p => p.ExsitsParameter(para));
- }
- /// <summary>
- /// 与当前相交的图元,快速过滤
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="elem"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, Element elem, double tolerance = 0)
- {
- BoundingBoxXYZ box = elem.get_BoundingBox(null);
- return box == null ? new List<Element>() : GetElements(doc, box, tolerance);
- }
- /// <summary>
- /// 相交的图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="box"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, BoundingBoxXYZ box, double tolerance = 0)
- {
- var otl = new Outline(box.Min, box.Max);
- return doc.GetElements(otl, tolerance);
- }
- /// <summary>
- /// 根据 BoundingBox获取特定类别相交图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="box"></param>
- /// <param name="category">类别</param>
- /// <param name="tolerance">误差值</param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, BoundingBoxXYZ box, BuiltInCategory category,
- double tolerance = 0)
- {
- Outline outline = new Outline(box.Min, box.Max);
- BoundingBoxIntersectsFilter boundFilter = tolerance.IsEqual(0)
- ? new BoundingBoxIntersectsFilter(outline)
- : new BoundingBoxIntersectsFilter(outline, tolerance);
- ElementCategoryFilter categoryFilter = new ElementCategoryFilter(category);
- LogicalAndFilter andFilter = new LogicalAndFilter(boundFilter, categoryFilter);
- return doc.GetElements(andFilter);
- }
- /// <summary>
- /// 根据 获取BoundingBox内特定类别图元(包含)
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="box"></param>
- /// <param name="category">类别</param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, BoundingBoxXYZ box, BuiltInCategory category)
- {
- Outline outline = new Outline(box.Min, box.Max);
- //BoundingBoxIntersectsFilter boundFilter = tolerance.IsEqual(0)
- // ? new BoundingBoxIntersectsFilter(outline)
- // : new BoundingBoxIntersectsFilter(outline, tolerance);
- ElementCategoryFilter categoryFilter = new ElementCategoryFilter(category);
- //return doc.GetElements(andFilter);
- // Create a BoundingBoxIsInside filter for Outline
- BoundingBoxIsInsideFilter filter = new BoundingBoxIsInsideFilter(outline);
- LogicalAndFilter andFilter = new LogicalAndFilter(filter, categoryFilter);
- // Apply the filter to the elements in the active document
- // This filter excludes all objects derived from View and objects derived from ElementType
- FilteredElementCollector collector = new FilteredElementCollector(doc);
- IList<Element> elements = collector.WherePasses(andFilter).ToElements();
- // Find walls outside BoundingBox: use an inverted filter to match elements
- // Use shortcut command OfClass() to find walls only
- BoundingBoxIsInsideFilter outsideFilter = new BoundingBoxIsInsideFilter(outline, true); // inverted filter
- collector = new FilteredElementCollector(doc);
- IList<Element> outsideFounds = collector.OfClass(typeof(Wall)).WherePasses(outsideFilter).ToElements();
- return elements.ToList();
- }
- /// <summary>
- /// 根据 BoundingBox获取指定类形相交图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="box"></param>
- /// <param name="type">指定类型</param>
- /// <param name="tolerance"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, BoundingBoxXYZ box, Type type, double tolerance = 0)
- {
- Outline outline = new Outline(box.Min, box.Max);
- BoundingBoxIntersectsFilter boundFilter = tolerance.IsEqual(0)
- ? new BoundingBoxIntersectsFilter(outline)
- : new BoundingBoxIntersectsFilter(outline, tolerance);
- return doc.GetElements(boundFilter).FindAll(p => p.GetType() == type);
- }
- /// <summary>
- /// 相交的图元
- /// 通过此方法可以找到相连接图元,如两个首尾相接的墙
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="otl"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, Outline otl, double tolerance = 0)
- {
- if (tolerance.IsEqual(0))
- {
- var boundFilter = new BoundingBoxIntersectsFilter(otl);
- return doc.FilterElements(boundFilter);
- }
- else
- {
- var boundFilter = new BoundingBoxIntersectsFilter(otl, tolerance);
- return doc.FilterElements(boundFilter);
- }
- }
- /// <summary>
- /// 获取图元,快速过滤
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="elem"></param>
- /// <param name="filter"></param>
- /// <param name="dSub"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, Element elem, ElementFilter filter, double dSub = 0)
- {
- var box = elem.get_BoundingBox(null);
- if (box == null)
- return new List<Element>();
- var vectSub = new XYZ(dSub, dSub, dSub);
- var otl = new Outline(box.Min.Add(vectSub), box.Max.Subtract(vectSub));
- return doc.GetElements(otl, filter);
- }
- /// <summary>
- /// 获取图元,快速过滤
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="otl"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, Outline otl, ElementFilter filter)
- {
- var boundFilter = new BoundingBoxIntersectsFilter(otl);
- var allFilter = new LogicalAndFilter(boundFilter, filter);
- return doc.FilterElements(allFilter);
- }
- /// <summary>
- /// 过滤器过滤图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, IEnumerable<Element> elements, ElementFilter filter)
- {
- return doc.FilterElements(elements, filter);
- }
- /// <summary>
- /// 过滤器过滤图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, ElementFilter filter)
- {
- return doc.FilterElements(filter);
- }
- public static List<Level> GetLevels(this Document doc)
- {
- List<Level> mLevels = null;
- mLevels = doc.FilterElements<Level>();
- mLevels.Sort(new CommonComparer<Level>((x, y) => { return x.Elevation.CompareTo(y.Elevation); }));
- return mLevels;
- }
- /// <summary>
- /// 返回激活层之间的所有Element
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static List<Element> GetElementsInActiveView(this Document doc)
- {
- var levelList = doc.GetLevels();
- var levelData = levelList.FindIndex(m => m.Id == doc.ActiveView.GenLevel.Id);
- doc.GetElements(doc.ActiveView.GenLevel);
- Level topLevel = null;
- if (levelData < levelList.Count - 1)
- {
- topLevel = levelList[levelData + 1];
- }
- List<Element> elements = new List<Element>();
- if (topLevel != null)
- {
- elements = doc.GetElements(doc.ActiveView.GenLevel, topLevel);
- }
- return elements;
- }
- public static List<Element> GetElements(this Document doc, IEnumerable<ElementId> ids)
- {
- List<Element> elements = new List<Element>();
- foreach (ElementId id in ids)
- {
- Element element = doc.GetElement(id);
- if (element != null)
- {
- elements.Add(element);
- }
- }
- return elements;
- }
- /// <summary>
- /// 框选楼板用
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="pickedBox">切记,PickedBox的Z轴有可能不是当前平面Z轴 </param>
- /// <param name="filter"></param>
- /// <param name="dZOffset"></param>
- /// <param name="dZExtend"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, PickedBox pickedBox, ElementFilter filter,
- double dZOffset, double dZExtend = 0)
- {
- ElementFilter ef = null;
- double x1 = Math.Max(pickedBox.Max.X, pickedBox.Min.X);
- double y1 = Math.Max(pickedBox.Max.Y, pickedBox.Min.Y);
- double x2 = Math.Min(pickedBox.Max.X, pickedBox.Min.X);
- double y2 = Math.Min(pickedBox.Max.Y, pickedBox.Min.Y);
- double z = pickedBox.Min.Z + dZOffset.ToApi();
- double dZExtendDown = 110d.ToApi(); //Z方向范围
- double dZExtendUp = 10d.ToApi(); //Z方向范围
- if (!dZExtend.IsZero(0))
- {
- dZExtendDown = dZExtend.ToApi();
- dZExtendUp = dZExtend.ToApi();
- }
- XYZ ptMin = new XYZ(x2, y2, z - dZExtendDown);
- XYZ ptMax = new XYZ(x1, y1, z + dZExtendUp);
- Outline otl = new Outline(ptMin, ptMax);
- if (filter != null)
- {
- ef = new LogicalAndFilter(filter, new BoundingBoxIntersectsFilter(otl));
- }
- else
- {
- ef = new BoundingBoxIntersectsFilter(otl);
- }
- return doc.FilterElements(ef);
- }
- /// <summary>
- /// 对应"在视图中可见的"对象
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="view"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, View view, ElementFilter filter = null)
- {
- FilteredElementCollector collector = new FilteredElementCollector(doc, view.Id);
- if (filter == null)
- {
- return collector.ToElements().ToList<Element>();
- }
- else
- {
- return collector.WherePasses(filter).ToElements().ToList<Element>();
- }
- }
- /// <summary>
- /// 返回视图中创建的对象,如类型标记等,可获取隐藏对象。
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="viewId"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, ElementId viewId)
- {
- FilteredElementCollector collector = new FilteredElementCollector(doc);
- return collector.OwnedByView(viewId).ToList<Element>();
- }
- /// <summary>
- /// 主要是层间对象
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="baseLevel"></param>
- /// <param name="topLevel"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, Level baseLevel, Level topLevel)
- {
- Outline otl = doc.CreateOutline(baseLevel, topLevel, 10, -10);
- return doc.GetElements(otl);
- }
- /// <summary>
- /// 两标高之间的图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="baseLevel">下标高</param>
- /// <param name="topLevel">上标高</param>
- /// <param name="baseOffset">下偏移</param>
- /// <param name="topOffset">上偏移</param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, Level baseLevel, Level topLevel, double baseOffset,
- double topOffset)
- {
- Outline otl = doc.CreateOutline(baseLevel, topLevel, baseOffset, topOffset);
- return doc.GetElements(otl);
- }
- /// <summary>
- /// 点获取图元
- /// 获取包含指定点的所有图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="pt"></param>
- /// <param name="tolerance">误差值</param>
- /// <returns></returns>
- /// ps:编译器只识别9位小数,而该方法默认识别13位小数,故需要指定误差值
- public static List<Element> GetElements(this Document doc, XYZ pt, double tolerance = 0)
- {
- return doc.FilterElements(new BoundingBoxContainsPointFilter(pt, tolerance));
- }
- /// <summary>
- /// 获取图元
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="pt"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static List<Element> GetElements(this Document doc, XYZ pt, ElementFilter filter)
- {
- List<Element> listRtn = new List<Element>();
- ElementFilter ef = new LogicalAndFilter(filter, new BoundingBoxContainsPointFilter(pt));
- listRtn = doc.FilterElements(ef);
- return listRtn;
- }
- /// <summary>
- /// 获取文字类型
- /// 由于符号用到 所以 从单行/多行文字中提出
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="strFont">文字字体</param>
- /// <param name="dFontHeight">字高</param>
- /// <param name="dHwRatio">宽度系数</param>
- /// <returns>返回文字类型</returns>
- public static TextNoteType GetTextNodeType(this Document doc, string strFont, double dFontHeight,
- double dHwRatio, string leaderArrowheadName = "实心箭头 30 度")
- {
- List<TextNoteType> types = doc.FilterElements<TextNoteType>();
- TextNoteType rtnType = null;
- string strTypename = string.Format("文字 {0} mm * {1} {2}", new object[] { dFontHeight, dHwRatio, strFont });
- if (types.Count > 0)
- {
- foreach (TextNoteType textType in types)
- {
- if (textType.Name == strTypename)
- {
- rtnType = textType;
- break;
- }
- }
- if (rtnType == null)
- {
- rtnType = types[0].DuplicateT<TextNoteType>(strTypename);
- //宽度系数
- rtnType.SetParameter(BuiltInParameter.TEXT_WIDTH_SCALE, dHwRatio);
- //文字大小
- rtnType.SetParameter(BuiltInParameter.TEXT_SIZE, dFontHeight.ToApi());
- //文字字体
- rtnType.SetParameter(BuiltInParameter.TEXT_FONT, strFont);
- //背景-透明
- rtnType.SetParameter(BuiltInParameter.TEXT_BACKGROUND, 1);
- //引线/边界偏移量
- rtnType.SetParameter(BuiltInParameter.LEADER_OFFSET_SHEET, 0.0);
- //引线箭头
- List<ElementType> listEleType =
- doc.FilterElements<ElementType>()
- .Where(d => d.Name.Equals(leaderArrowheadName))
- .ToList<ElementType>();
- if ((listEleType != null) && (listEleType.Count > 0))
- {
- rtnType.SetParameter(BuiltInParameter.LEADER_ARROWHEAD, listEleType[0].Id);
- }
- }
- }
- return rtnType;
- }
- public static TextNoteType GetTextNodeType(this Document doc, string strTypeName)
- {
- TextNoteType textNodeType = doc.FindTextNodeType(strTypeName, true);
- return textNodeType;
- }
- public static TextNoteType FindTextNodeType(this Document doc, string strTypeName)
- {
- return doc.FindTextNodeType(strTypeName, false);
- }
- /// <summary>
- /// 文字类型
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static List<TextNoteType> GetTextNoteTypeSet(this Document doc)
- {
- return doc.FilterElements<TextNoteType>();
- }
- public static TextNoteType FindTextNodeType(this Document doc, string strTypeName, bool notFindReturnDefault)
- {
- TextNoteType returnDefault = null;
- List<TextNoteType> textNodeTypes = doc.GetTextNoteTypeSet();
- foreach (TextNoteType textNodeType in textNodeTypes)
- {
- if (textNodeType.Name == strTypeName)
- {
- return textNodeType;
- }
- if (returnDefault == null)
- returnDefault = textNodeType;
- }
- if (notFindReturnDefault)
- return returnDefault;
- return null;
- }
- /// <summary>
- /// 标注类型
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static List<DimensionType> GetDimensionTypeSet(this Document doc)
- {
- return doc.FilterElements<DimensionType>();
- }
- /// <summary>
- /// 获取由用户自己配置的标注样式
- /// 注:根据文档要求,项目中所有用到的标注样式都从该方法获取
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- //public static DimensionType GetDimensionType(this Document doc)
- //{
- // return doc.GetDimensionType(null);
- //}
- /// <summary>
- /// 获取标注默认配置
- /// </summary>
- /// <returns></returns>
- private static string GetDimensionDefConfig()
- {
- /*
- * 根据文档<<编辑工具功能设定>>要求,标注使用以下默认值 根据谭工要求,重新设定默认值
- * 两侧出头:2.5(mm) 两侧出头:1(mm)
- * 终端出头:2.5(mm) 终端出头:2(mm)
- * 引出线长:3.5(mm) 引出线长:3.5(mm)
- * 字线间距:1.5(mm) 字线间距:0.5(mm)
- * 粗线宽度:4(mm) 粗线宽度:6(mm)
- * 粗线长度:3(mm) 粗线长度:1.24(mm)
- * 标注字高:3(mm) 标注字高:3.5(mm)
- * 标注字体:SAGArevit 标注字体:Dotum
- * 标注颜色:无 标注颜色:绿
- */
- string strDefault = "Dotum"; // RevitUtils.GetSAGAFontName();
- string defaultConfig = ",1.24,6,3.5,0.5,2,3.5," + strDefault + ",1,65280,";
- return defaultConfig;
- }
- /// <summary>
- /// 获取一个标注的参数
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static string GetDimensionConfig(this DimensionType type)
- {
- double cxcd =
- type.GetParameterElement(BuiltInParameter.DIM_LEADER_ARROWHEAD)
- .GetParameterDoubleMm(BuiltInParameter.ARROW_SIZE);
- double cxkd = type.GetParameterInteger(BuiltInParameter.TICK_MARK_PEN);
- double bzzg = type.GetParameterDoubleMm(BuiltInParameter.TEXT_SIZE);
- double zxjj = type.GetParameterDoubleMm(BuiltInParameter.TEXT_DIST_TO_LINE);
- double zdct = type.GetParameterDoubleMm(BuiltInParameter.WITNS_LINE_EXTENSION);
- double ycxc = type.GetParameterDoubleMm(BuiltInParameter.DIM_WITNS_LINE_EXTENSION_BELOW);
- string strDefault = type.GetParameterString(BuiltInParameter.TEXT_FONT);
- double lcct = type.GetParameterDoubleMm(BuiltInParameter.DIM_LINE_EXTENSION);
- double color = type.GetParameterInteger(BuiltInParameter.LINE_COLOR);
- string result = string.Format(",{0},{1},{2},{3},{4},{5},{6},{7},{8}", cxcd, cxkd, bzzg, zxjj, zdct, ycxc,
- strDefault, lcct, color);
- return result;
- }
- /// <summary>
- /// 线创建标注
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="line"></param>
- /// <param name="view"></param>
- /// <param name="dType"></param>
- /// <returns></returns>
- public static Dimension CreateDimension(this Document doc, Line line, View view, DimensionType dType = null)
- {
- List<XYZ> ptList = new List<XYZ>();
- ptList.Add(line.StartPoint());
- ptList.Add(line.EndPoint());
- Dimension dim = doc.CreateDimension(line, ptList, view);
- if (dim != null && dType != null)
- {
- dim.DimensionType = dType;
- }
- return dim;
- }
- /// <summary>
- /// 线上点创建标注
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="line"></param>
- /// <param name="ptList"></param>
- /// <param name="view"></param>
- /// <param name="dType"></param>
- /// <returns></returns>
- public static Dimension CreateDimension(this Document doc, Line line, List<XYZ> ptList, View view,
- DimensionType dType = null)
- {
- Dimension result = null;
- ReferenceArray array = new ReferenceArray();
- if (ptList != null && ptList.Count > 0)
- {
- //参照线方向
- XYZ vector = line.Direction.VectorRotate(view.ViewDirection, Math.PI / 2);
- foreach (XYZ item in ptList)
- {
- Reference refer = doc.GetReferenceDetail(item, vector, view);
- if (refer != null)
- {
- array.Append(refer);
- }
- }
- if (array.Size > 1)
- {
- result = doc.Create.NewDimension(view, line, array);
- if (result != null && dType != null)
- {
- result.DimensionType = dType;
- }
- }
- }
- return result;
- }
- /// <summary>
- /// 通过矩离创建标注
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="line"></param>
- /// <param name="listSpace"></param>
- /// <param name="view"></param>
- /// <param name="dType"></param>
- /// <returns></returns>
- public static Dimension CreateDimension(this Document doc, Line line, List<double> listSpace, View view,
- DimensionType dType = null)
- {
- double dStart = 0;
- List<XYZ> listPt = new List<XYZ>();
- for (int i = 0; i < listSpace.Count; i++)
- {
- double dSpace = listSpace[i];
- listPt.Add(line.StartPoint() + line.Direction * dStart);
- dStart += dSpace;
- }
- listPt.Add(line.StartPoint() + line.Direction * dStart);
- return doc.CreateDimension(line, listPt, view, dType);
- }
- /// <summary>
- /// 两道尺寸标注 测试用
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="line"></param>
- /// <param name="listFirstSpace"></param>
- /// <param name="listSecondSpace"></param>
- /// <param name="view"></param>
- /// <returns></returns>
- public static List<Dimension> CreateDimension(this Document doc, Line line, List<double> listFirstSpace,
- List<double> listSecondSpace, View view)
- {
- List<Dimension> listRtn = new List<Dimension>();
- //参照线方向
- XYZ vector = line.Direction.VectorRotate(view.ViewDirection, Math.PI / 2);
- double dStart = 0;
- List<XYZ> listPtFirst = new List<XYZ>();
- for (int i = 0; i < listFirstSpace.Count; i++)
- {
- double dSpace = listFirstSpace[i].ToApi();
- listPtFirst.Add(line.StartPoint() + line.Direction * dStart);
- dStart += dSpace;
- }
- listPtFirst.Add(line.StartPoint() + line.Direction * dStart);
- dStart = 0;
- List<XYZ> listPtSecond = new List<XYZ>();
- for (int i = 0; i < listSecondSpace.Count; i++)
- {
- double dSpace = listSecondSpace[i].ToApi();
- listPtSecond.Add(line.StartPoint() + line.Direction * dStart);
- dStart += dSpace;
- }
- listPtSecond.Add(line.StartPoint() + line.Direction * dStart);
- List<XYZ> listPt = new List<XYZ>();
- listPt.AddRange(listPtFirst);
- foreach (var pt in listPtSecond)
- {
- if (!pt.ExsitsInlist(listPt))
- {
- listPt.Add(pt);
- }
- }
- Dictionary<int, Reference> dicRef = new Dictionary<int, Reference>();
- for (int i = 0; i < listPt.Count; i++)
- {
- XYZ item = listPt[i];
- Reference refer = doc.GetReferenceDetail(item, vector, view);
- if (refer != null)
- {
- dicRef.Add(i, refer);
- }
- }
- ReferenceArray array = new ReferenceArray();
- foreach (var pt in listPtFirst)
- {
- int index = listPt.FindIndex(p => p.IsEqual(pt));
- array.Append(dicRef[index]);
- }
- if (array.Size > 1)
- {
- Dimension result = doc.Create.NewDimension(view, line, array);
- listRtn.Add(result);
- }
- //两个字高
- double dOffset = (3.0 * 2 * view.Scale).ToApi();
- // line = line.Offset(dOffset);
- array = new ReferenceArray();
- foreach (var pt in listPtSecond)
- {
- int index = listPt.FindIndex(p => p.IsEqual(pt));
- array.Append(dicRef[index]);
- }
- if (array.Size > 1)
- {
- Dimension result = doc.Create.NewDimension(view, line, array);
- listRtn.Add(result);
- }
- return listRtn;
- }
- /// <summary>
- /// 创建标注的短参照
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="start"></param>
- /// <param name="vector"></param>
- /// <param name="view"></param>
- /// <returns></returns>
- public static Reference GetReferenceDetail(this Document doc, XYZ start, XYZ vector, View view = null)
- {
- Reference result = null;
- if (view == null)
- {
- view = doc.ActiveView;
- }
- Line line = vector.VectorToLine(start, 0.02 /*d.ToApi()*/);
- if (view.ViewType == ViewType.ThreeD)
- {
- ModelCurve mc = LineExtend.NewModelCurve(line);
- if (mc != null)
- {
- result = new Reference(mc);
- }
- }
- else
- {
- DetailCurve dc = doc.Create.NewDetailCurve(view, line);
- if (dc != null)
- {
- result = new Reference(dc);
- }
- }
- return result;
- }
- /// <summary>
- /// 获取标注配置
- /// </summary>
- /// <returns></returns>
- private static string GetDimensionConfig()
- {
- //文件路径
- string filePath = AppBaseInfo.AppTempFilePath + "\\ConfigDimensionType.dll";
- string config = null;
- #region 读取配置
- if (File.Exists(filePath))
- {
- //存在本地文件 读取本地文件配置
- using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read))
- {
- using (StreamReader sr = new StreamReader(fs, Encoding.Default))
- {
- config = sr.ReadLine();
- sr.Close();
- }
- fs.Close();
- }
- }
- #endregion
- return config;
- }
- /// <summary>
- /// 获取用户设置的标注样式
- /// 该方法可设置尺寸界线长度
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="length"></param>
- /// <returns></returns>
- //public static DimensionType GetDimensionType(this Document doc, double? length = null, string config = null)
- //{
- // string defaultConfig = GetDimensionDefConfig();
- // if (string.IsNullOrEmpty(config))
- // config = GetDimensionConfig();
- // bool isSave = true;
- // if (length != null && length.Value > 0)
- // {
- // isSave = false;
- // string[] array = config.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
- // if (array.Length > 5)
- // {
- // array[5] = length.Value.ToString();
- // config = ConvertToConfig(array);
- // }
- // array = defaultConfig.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
- // if (array.Length > 5)
- // {
- // array[5] = length.Value.ToString();
- // defaultConfig = ConvertToConfig(array);
- // }
- // }
- // DimensionType result = CreateDimensionType(doc, config, isSave);
- // if (result == null)
- // result = CreateDimensionType(doc, defaultConfig, isSave);
- // return result;
- //}
- private static string ConvertToConfig(string[] array)
- {
- string config = ",";
- foreach (string item in array)
- {
- config += item + ",";
- }
- return config;
- }
- public static DimensionType FindDimensionType(this Document doc, string strTypeName)
- {
- DimensionType result = null;
- List<DimensionType> dimTypes = doc.GetDimensionTypeSet();
- foreach (DimensionType dimType in dimTypes)
- {
- /*
- * 解决问题:在没有找到标注类型时,在不同模板中不能直接默认取第一个
- * 应该取类型一致的
- * DimensionStyleType弧长和角度标注有做的可能
- */
- if (result == null && dimType.StyleType == DimensionStyleType.Linear)
- result = dimType;
- if (!string.IsNullOrEmpty(dimType.Name)
- && dimType.Name.Replace(" ", "").Trim().Equals(strTypeName.Replace(" ", "").Trim()))
- {
- return dimType;
- }
- }
- return result;
- }
- public static bool CanMirrorElement(this Document doc, ElementId elemId)
- {
- return ElementTransformUtils.CanMirrorElement(doc, elemId);
- }
- public static bool CanMirrorElements(this Document doc, ICollection<ElementId> elemIds)
- {
- return ElementTransformUtils.CanMirrorElements(doc, elemIds);
- }
- public static ICollection<ElementId> CopyElement(this Document doc, ElementId elem, XYZ translation)
- {
- return ElementTransformUtils.CopyElement(doc, elem, translation);
- }
- public static ICollection<ElementId> CopyElements(this Document doc, ICollection<ElementId> elementsToCopy,
- XYZ translation)
- {
- return ElementTransformUtils.CopyElements(doc, elementsToCopy, translation);
- }
- public static void MirrorElement(this Document doc, ElementId elementToMirror, Plane plane)
- {
- ElementTransformUtils.MirrorElement(doc, elementToMirror, plane);
- }
- public static void MirrorElements(this Document doc, ICollection<ElementId> elementsToMirror, Plane plane)
- {
- ElementTransformUtils.MirrorElements(doc, elementsToMirror, plane, true);
- }
- public static void MoveElement(this Document doc, ElementId elementToMove, XYZ translation)
- {
- ElementTransformUtils.MoveElement(doc, elementToMove, translation);
- }
- public static void MoveElements(this Document doc, ICollection<ElementId> elem, XYZ translation)
- {
- ElementTransformUtils.MoveElements(doc, elem, translation);
- }
- public static void RotateElement(this Document doc, Element elem, XYZ pt, double angle)
- {
- Line axis = Line.CreateBound(pt, pt.AddZ(1));
- doc.RotateElement(elem, axis, angle);
- }
- public static void RotateElement(this Document doc, Element elem, Line axis, double angle)
- {
- ElementTransformUtils.RotateElement(doc, elem.Id, axis, angle);
- }
- public static void RotateElement(this Document doc, ElementId elem, Line axis, double angle)
- {
- ElementTransformUtils.RotateElement(doc, elem, axis, angle);
- }
- public static void RotateElements(this Document doc, ICollection<ElementId> elementsToRotate, Line axis,
- double angle)
- {
- ElementTransformUtils.RotateElements(doc, elementsToRotate, axis, angle);
- }
- public static void RotateElements(this Document doc, ICollection<Element> elementsToRotate, Line axis,
- double angle)
- {
- List<ElementId> elementIdsToRotate = new List<ElementId>();
- foreach (Element e in elementsToRotate)
- {
- elementIdsToRotate.Add(e.Id);
- }
- ElementTransformUtils.RotateElements(doc, elementIdsToRotate, axis, angle);
- }
- /// <summary>
- /// 创建明细表
- /// </summary>
- /// <param name="viewScheduleName">明细表名称</param>
- /// <param name="filedArray">明细表字段名集合</param>
- ///<param name="isSharedParam">是否取共享参数</param>
- /// <returns></returns>
- public static ViewSchedule CreateViewSchedule(this Document doc, string viewScheduleName,
- BuiltInCategory category, string[] filedArray, ElementId areaSchemaId = null)
- {
- List<ViewSchedule> scheduleList = doc.GetElements(typeof(ViewSchedule)).ToList<ViewSchedule>();
- ViewSchedule schedule = scheduleList.Find(p => p.Name == viewScheduleName);
- if (schedule != null)
- {
- return schedule;
- }
- Transaction trans = new Transaction(doc, "CreateViewSchedule");
- trans.Start();
- try
- {
- if (areaSchemaId == null)
- schedule = ViewSchedule.CreateSchedule(doc, new ElementId(category), ElementId.InvalidElementId);
- else
- schedule = ViewSchedule.CreateSchedule(doc, new ElementId(category), areaSchemaId);
- #region 创建明细表表头
- TableData tableData = schedule.GetTableData();
- if (tableData != null)
- {
- TableSectionData sectionData = tableData.GetSectionData(0);
- if (sectionData != null && sectionData.NumberOfColumns == 0 && sectionData.NumberOfRows == 0)
- {
- //#if R16
- sectionData.InsertColumn(0);
- //#else
- //sectionData.InsertColumn(0, true);
- //#endif
- sectionData.SetColumnWidth(0, tableData.Width);
- doc.Regenerate();
- sectionData.InsertRow(0);
- sectionData.SetCellText(0, 0, viewScheduleName);
- }
- }
- #endregion
- schedule.Name = viewScheduleName;
- ScheduleDefinition definition = schedule.Definition;
- definition.ShowGrandTotal = true;
- definition.ShowGrandTotalTitle = true;
- List<SchedulableField> filedList = schedule.Definition.GetSchedulableFields().ToList();
- foreach (string s in filedArray)
- {
- SchedulableField filed = filedList.Find(p => p.GetName(doc) == s);
- if (filed != null)
- {
- ScheduleField scheduleField = schedule.Definition.AddField(filed);
- if (s.Contains("SAGA")) scheduleField.ColumnHeading = s.Substring(3);
- if (scheduleField.CanTotal())
- {
- scheduleField.SetHasTotals();
- }
- }
- }
- trans.Commit();
- return schedule;
- }
- catch
- {
- trans.RollBack();
- }
- return null;
- }
- /// <summary>
- /// 删除扩展
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="element"></param>
- public static void DeleteExt(this Document doc, Element element)
- {
- //修改原因:Delete(element.Id)不能删除其上的子对象
- element.Delete();
- }
- /// <summary>
- /// 计算文本标签的宽度
- /// (需在事物中调用)
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="view"></param>
- /// <param name="str"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- // public static double ComputeTextNoteWidth(this Document doc, View view, string str,
- // TextNoteType type, double singleWidth = 0)
- // {
- // double d1 = 0;
- // if (singleWidth == 0)
- // singleWidth = doc.ComputeSingleWidth(view, str, type);
- // d1 = singleWidth;
- // if (str.Contains('\n'))
- // {
- // List<string> list = new List<string>();
- // list.AddRange(str.Split('\n'));
- // str = list.OrderByDescending(s => s.Length).First();
- // }
- // //一个汉字占两个字符位
- // int k = Encoding.Default.GetByteCount(str);
- // //暂时未计算探索者钢筋符号的信息,探索者钢筋符号中一个符号占一个字符位,但是大小为两个字符位的大小
- // //如果包含空格,每多一个空格,增加一个字符宽度
- // char[] charArray = str.ToArray();
- // int count = 0;
- // foreach (char item in charArray)
- // {
- // if (string.IsNullOrEmpty(item.ToString().Trim()))
- // {
- // count++;
- // }
- // }
- //#if R15
- // count++;//15中的字符末尾会多出一个空格
- // k++;
- //#endif
- // return d1*(k + count);
- // }
- ///// <summary>
- ///// 创建标注
- ///// </summary>
- ///// <param name="doc"></param>
- ///// <param name="list">所有详图线</param>
- ///// <param name="start">标注起点(转换前坐标)</param>
- ///// <param name="end">标注终点(转换前坐标)</param>
- ///// <param name="dimPoint">标注点(转换前坐标)</param>
- ///// <param name="drawView">视图</param>
- //public static Dimension CreateDimension(this Document doc, List<DetailCurve> list, XYZ start, XYZ end,
- // XYZ dimPoint, View drawView, string showText = null, DimensionType type = null)
- //{
- // Line line = Line.CreateBound(start, end);
- // XYZ foot = line.VerticalPoint(dimPoint);
- // XYZ vector = dimPoint.Subtract(foot).Normalize();
- // if (vector.IsEqual(XYZ.Zero))
- // {
- // vector = line.GetVUnboundLine(start).LineToBound().UnitVector().Normalize();
- // }
- // line = line.OffsetPoint(dimPoint);
- // Reference startRef = start.GetReferenceByDetailList(list, vector, drawView);
- // Reference endRef = end.GetReferenceByDetailList(list, vector, drawView);
- // if (startRef != null && endRef != null)
- // {
- // ReferenceArray array = new ReferenceArray();
- // array.Append(startRef);
- // array.Append(endRef);
- // Dimension dim = null;
- // if (type != null)
- // dim = doc.Create.NewDimension(drawView, line, array, type);
- // else
- // dim = doc.Create.NewDimension(drawView, line, array);
- // if (dim != null)
- // {
- // if (showText != null && !showText.Equals("<>"))
- // dim.ValueOverride = showText;
- // }
- // return dim;
- // }
- // return null;
- //}
- //public static Reference GetReferenceByDetailList(this XYZ point, List<DetailCurve> detailList, XYZ vector,
- // View drawView)
- //{
- // if (detailList == null)
- // detailList = new List<DetailCurve>();
- // Reference refLine = null;
- // XYZ end = point + vector*0.015 /*d.ToApi()*/;
- // Line line = Line.CreateBound(point, end);
- // DetailCurve dc = detailList.Find(p => p.GeometryCurve.IsOnCurve(line));
- // if (dc != null)
- // refLine = new Reference(dc);
- // else //没有的补一根线
- // {
- // CurveArray ca = new CurveArray();
- // ca.Append(line);
- // List<DetailCurve> list = ca.DrawDetailLines(drawView);
- // if (list != null && list.Count > 0)
- // refLine = new Reference(list[0]);
- // }
- // return refLine;
- //}
- /// <summary>
- /// 获取一个标注的所有参照
- /// (提出该方法理由:从标注中直接取出的参照在16版本不能直接使用)
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="dim"></param>
- /// <returns></returns>
- public static ReferenceArray GetReferences(this Dimension dim)
- {
- #if R16
- ReferenceArray array = new ReferenceArray();
- foreach (Reference item in dim.References)
- {
- Element elem = ExternalDataWrapper.Current.Doc.GetElement(item);
- if (elem != null)
- array.Append(new Reference(elem));
- }
- #else
- ReferenceArray array = dim.References;
- #endif
- return array;
- }
- public static List<SlabEdgeType> GetSlabEdgeTypesEx(this Document doc)
- {
- FilteredElementCollector collector = new FilteredElementCollector(doc);
- collector.OfClass(typeof(SlabEdgeType));
- return collector.OfType<SlabEdgeType>().ToList();
- }
- public static List<RoomTagType> RoomTagTypesEx(this Document doc)
- {
- FilteredElementCollector collector = new FilteredElementCollector(doc);
- collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_RoomTags);
- return collector.OfType<RoomTagType>().ToList();
- }
- public static List<T> GetElementsT<T>(this Document doc, IEnumerable<ElementId> elementIds) where T : Element
- {
- List<T> listElements = new List<T>();
- if (elementIds == null)
- return listElements;
- foreach (ElementId elementId in elementIds)
- {
- T tv = doc.GetElementT<T>(elementId);
- if (tv != null)
- listElements.Add(tv);
- }
- return listElements;
- }
- }
- public class SAGAFamilyLoadOptions : IFamilyLoadOptions
- {
- public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
- {
- overwriteParameterValues = true;
- return true;
- }
- public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source,
- out bool overwriteParameterValues)
- {
- source = FamilySource.Family;
- overwriteParameterValues = true;
- return true;
- }
- }
- public enum DrivenTypes
- {
- NotSupport = 0,
- Point,
- Curve,
- Face
- }
- }
|