public static IEnumerable <Element> GetFamilyInstancesFromDividedSurface(DividedSurface ds) { var gn = new GridNode(); var u = 0; while (u < ds.NumberOfUGridlines) { gn.UIndex = u; var v = 0; while (v < ds.NumberOfVGridlines) { gn.VIndex = v; //"Reports whether a grid node is a "seed node," a node that is associated with one or more tiles." if (ds.IsSeedNode(gn)) { var fi = ds.GetTileFamilyInstance(gn, 0); if (fi != null) { //put the family instance into the tree yield return(fi); } } v = v + 1; } u = u + 1; } }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; IList <Reference> referenceCollection = uidoc.Selection.PickObjects(ObjectType.Face); MessageBox.Show("You have selected total " + referenceCollection.Count.ToString() + " faces."); Transaction trans = new Transaction(doc); trans.Start("建立網格"); foreach (Reference face in referenceCollection) { DividedSurface ds = DividedSurface.Create(doc, face); SpacingRule srU = ds.USpacingRule; srU.SetLayoutFixedDistance(1, SpacingRuleJustification.Center, 0, 0); SpacingRule srV = ds.VSpacingRule; srV.SetLayoutFixedDistance(1, SpacingRuleJustification.Center, 0, 0); Element host = ds.Host; Face dividefaces = host.GetGeometryObjectFromReference(ds.HostReference) as Face; GridNode node = new GridNode(); UV uv = ds.GetGridNodeUV(node); XYZ pt = dividefaces.Evaluate(uv); MessageBox.Show(pt.ToString()); } trans.Commit(); return(Result.Succeeded); }
/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view.</param> /// <param name="message">A message that can be set by the external application /// which will be displayed if a failure or cancellation is returned by /// the external command.</param> /// <param name="elements">A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation.</param> /// <returns>Return the status of the external command. /// A result of Succeeded means that the API external method functioned as expected. /// Cancelled can be used to signify that the user cancelled the external operation /// at some point. Failure should be returned if the application is unable to proceed with /// the operation.</returns> public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application; Document doc = commandData.Application.ActiveUIDocument.Document; Parameter param = null; Bitmap image = new Bitmap(doc.PathName + "_grayscale.bmp"); FilteredElementCollector collector = new FilteredElementCollector(doc); ICollection <Element> collection = collector.OfClass(typeof(DividedSurface)).ToElements(); foreach (Element element in collection) { DividedSurface ds = element as DividedSurface; GridNode gn = new GridNode(); for (int u = 0; u < ds.NumberOfUGridlines; u++) { gn.UIndex = u; for (int v = 0; v < ds.NumberOfVGridlines; v++) { gn.VIndex = v; if (ds.IsSeedNode(gn)) { FamilyInstance familyinstance = ds.GetTileFamilyInstance(gn, 0); if (familyinstance != null) { param = familyinstance.get_Parameter("Grayscale"); if (param == null) { throw new Exception("Panel family must have a Grayscale instance parameter"); } else { System.Drawing.Color pixelColor = new System.Drawing.Color(); try { pixelColor = image.GetPixel(image.Width - v, image.Height - u); double grayscale = 255 - ((pixelColor.R + pixelColor.G + pixelColor.B) / 3); if (grayscale == 0) { doc.Delete(familyinstance.Id); } else { param.Set(grayscale / 255); } } catch (System.Exception) { // TaskDialog.Show("Revit", "Exception: " + u + ", " + v); } } } } } } } doc.Regenerate();; return(Result.Succeeded); }
/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view.</param> /// <param name="message">A message that can be set by the external application /// which will be displayed if a failure or cancellation is returned by /// the external command.</param> /// <param name="elements">A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation.</param> /// <returns>Return the status of the external command. /// A result of Succeeded means that the API external method functioned as expected. /// Cancelled can be used to signify that the user cancelled the external operation /// at some point. Failure should be returned if the application is unable to proceed with /// the operation.</returns> public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { // store the active Revit document m_document = commandData.Application.ActiveUIDocument.Document; DividedSurface ds = GetDividedSurface(); if (null == ds) { message = "Open the family file from the sample folder first."; return(Result.Failed); } IEnumerable <ElementId> planes = GetPlanes(); IEnumerable <ElementId> lines = GetLines(); Transaction act = new Transaction(m_document); act.Start("AddRemoveIntersects"); try { // step 1: divide the surface with reference planes and levels foreach (ElementId id in planes) { if (ds.CanBeIntersectionElement(id)) { ds.AddIntersectionElement(id); } } // step 2: remove all the reference planes and level intersection elements IEnumerable <ElementId> intersects = ds.GetAllIntersectionElements(); foreach (ElementId id in intersects) { ds.RemoveIntersectionElement(id); } // step 3: divide the surface with model lines instead foreach (ElementId id in lines) { if (ds.CanBeIntersectionElement(id)) { ds.AddIntersectionElement(id); } } } catch (Exception) { act.RollBack(); } finally { act.Commit(); } return(Autodesk.Revit.UI.Result.Succeeded); }
public void ByFaceUVDivisions_NullArgument() { ElementFaceReference faceRef = null; Assert.Throws(typeof(ArgumentNullException), () => DividedSurface.ByFaceAndUVDivisions(faceRef, 5, 5)); Autodesk.DesignScript.Geometry.Surface srf = null; Assert.Throws(typeof(ArgumentNullException), () => DividedSurface.ByFaceAndUVDivisions(srf, 5, 5)); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; try { // find forms in the model: FilteredElementCollector forms = new FilteredElementCollector(doc); forms.OfClass(typeof(Form)); foreach (Form form in forms) { // access the divided surface data from the form: //DividedSurfaceData dsData = form.GetDividedSurfaceData(); // before 2014 //if( null != dsData ) { // get the references associated with the divided surfaces //foreach( Reference reference in dsData.GetReferencesWithDividedSurfaces() ) foreach (Reference reference in DividedSurface.GetReferencesWithDividedSurfaces(form)) { DividedSurface divSurface = DividedSurface.GetDividedSurfaceForReference(doc, reference); int count = 0; TilePatterns tilepatterns = doc.Settings.TilePatterns; foreach (TilePatternsBuiltIn i in Enum.GetValues(typeof(TilePatternsBuiltIn))) { if (3 == count) { // Warning: 'Autodesk.Revit.DB.Element.ObjectType' is obsolete: // 'Use Element.GetTypeId() and Element.ChangeTypeId() instead.' // //divSurface.ObjectType = tilepatterns.GetTilePattern( i ); divSurface.ChangeTypeId(tilepatterns.GetTilePattern(i).Id); break; } ++count; } } } } } catch (Exception ex) { message = ex.Message; return(Result.Failed); } return(Result.Succeeded); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; Autodesk.Revit.Creation.Application creApp = app.Application.Create; try { // find forms in the model: FilteredElementCollector forms = new FilteredElementCollector(doc); forms.OfCategory(BuiltInCategory.OST_MassForm); // was OST_MassSurface foreach (Form form in forms) { // create the divided surface on the loft form: FamilyItemFactory factory = doc.FamilyCreate; Options options = creApp.NewGeometryOptions(); options.ComputeReferences = true; options.View = doc.ActiveView; GeometryElement element = form.get_Geometry(options); //GeometryObjectArray geoObjectArray = element.Objects; // 2012 //for( int j = 0; j < geoObjectArray.Size; j++ ) //{ // GeometryObject geoObject = geoObjectArray.get_Item( j ); foreach (GeometryObject geoObject in element) // 2013 { Solid solid = geoObject as Solid; foreach (Face face in solid.Faces) { if (face.Reference != null) { if (null != face) { //DividedSurface divSurface = factory.NewDividedSurface( face.Reference ); // 2013 DividedSurface divSurface = DividedSurface.Create(doc, face.Reference); // 2014 } } } } } return(Result.Succeeded); } catch (Exception ex) { message = ex.Message; return(Result.Failed); } }
protected Element SafeGetHost(DividedSurface element) { var host = element.Host; if (host != null && host.Id == element.Id) { //某些情况下, 构件的 Host 就是它自己, 会导致无限循环递归调用, 直至堆栈溢出 host = null; } return(host); }
public void ByFaceUVDivisions_InvalidDivisions() { var ele = ElementSelector.ByType <Autodesk.Revit.DB.Form>(true).FirstOrDefault(); Assert.NotNull(ele); var form = ele as Form; var face = form.ElementFaceReferences.First(); Assert.Throws(typeof(Exception), () => DividedSurface.ByFaceAndUVDivisions(face, 5, 0)); Assert.Throws(typeof(Exception), () => DividedSurface.ByFaceAndUVDivisions(face, 0, 5)); Assert.Throws(typeof(Exception), () => DividedSurface.ByFaceAndUVDivisions(face, 0, 0)); }
public void ByFaceUVDivisionsRotation_ValidArgs() { var ele = ElementSelector.ByType <Autodesk.Revit.DB.Form>(true).FirstOrDefault(); Assert.NotNull(ele); var form = ele as Form; var face = form.ElementFaceReferences.First(); var divSrf = DividedSurface.ByFaceUVDivisionsAndRotation(face, 5, 6, 30); Assert.NotNull(divSrf); Assert.AreEqual(5, divSrf.UDivisions); Assert.AreEqual(6, divSrf.VDivisions); Assert.AreEqual(30.0, divSrf.Rotation, 0.001); }
/// <summary> /// Get all the panel instances from a divided surface /// </summary> /// <param name="ds">The divided surface with some panels</param> /// <returns>A list containing all the panel instances</returns> private List <FamilyInstance> GetFamilyInstances(DividedSurface ds) { List <FamilyInstance> fiList = new List <FamilyInstance>(); for (int u = 0; u < ds.NumberOfUGridlines; ++u) { for (int v = 0; v < ds.NumberOfVGridlines; ++v) { GridNode gn = new GridNode(u, v); FamilyInstance familyInstance = ds.GetTileFamilyInstance(gn, 0); if (familyInstance != null) { fiList.Add(familyInstance); } } } return(fiList); }
/// <summary> /// Populate DividedSurfaceArray with the selected surfaces or all surfaces in the model /// </summary> private void GetDividedSurfaces() { // want to compute all the divided surfaces if (m_uiDoc.Selection.Elements.Size == 0) { m_dividedSurfaceList = GetElements <DividedSurface>(); return; } // user selects some divided surface foreach (Element element in m_uiDoc.Selection.Elements) { DividedSurface ds = element as DividedSurface; if (ds != null) { m_dividedSurfaceList.Add(ds); } } }
/// <summary> /// Populate DividedSurfaceArray with the selected surfaces or all surfaces in the model /// </summary> private void GetDividedSurfaces() { // want to compute all the divided surfaces if (m_uiDoc.Selection.GetElementIds().Count == 0) { m_dividedSurfaceList = GetElements <DividedSurface>(); return; } // user selects some divided surface foreach (ElementId elementId in m_uiDoc.Selection.GetElementIds()) { Element element = m_uiDoc.Document.GetElement(elementId); DividedSurface ds = element as DividedSurface; if (ds != null) { m_dividedSurfaceList.Add(ds); } } }
public static DividedSurface RequestDividedSurfaceSelection(string message) { var doc = DocumentManager.Instance.CurrentUIDocument; DividedSurface f = null; Selection choices = doc.Selection; choices.Elements.Clear(); dynSettings.Controller.DynamoLogger.Log(message); Reference formRef = doc.Selection.PickObject(ObjectType.Element); if (formRef != null) { //get the element var el = DocumentManager.Instance.CurrentDBDocument.GetElement(formRef); f = el as DividedSurface; } return(f); }
public static DividedSurface RequestDividedSurfaceSelection(string message, ILogger logger) { var doc = DocumentManager.Instance.CurrentUIDocument; DividedSurface f = null; var choices = doc.Selection; choices.Elements.Clear(); logger.Log(message); var formRef = doc.Selection.PickObject( ObjectType.Element, new DividedSurfaceSelectionFilter()); if (formRef != null) { //get the element var el = DocumentManager.Instance.CurrentDBDocument.GetElement(formRef); f = (DividedSurface)el; } return(f); }
/// <summary> /// Check all the panels whose areas are below/above/within the range in the divided surface, mark them with different symbols /// </summary> /// <param name="ds"> /// The divided surfaces created in the document, it contains the panels for checking /// </param> void ExamineDividedSurface(DividedSurface ds) { ElementType sym = ds.Document.GetElement(ds.GetTypeId()) as ElementType; FamilySymbol fs_min = null; FamilySymbol fs_max = null; FamilySymbol fs_mid = null; // get the panel types which are used to identify the panels in the divided surface FamilySymbol fs = sym as FamilySymbol; foreach (ElementId symbolId in fs.Family.GetFamilySymbolIds()) { FamilySymbol symbol = (FamilySymbol)m_uiDoc.Document.GetElement(symbolId); if (symbol.Name == m_maxType) { fs_max = symbol; } if (symbol.Name == m_minType) { fs_min = symbol; } if (symbol.Name == m_midType) { fs_mid = symbol; } } // find all the panels areas and compare with the range for (int u = 0; u < ds.NumberOfUGridlines; u++) { for (int v = 0; v < ds.NumberOfVGridlines; v++) { GridNode gn = new GridNode(u, v); if (false == ds.IsSeedNode(gn)) { continue; } FamilyInstance familyinstance = ds.GetTileFamilyInstance(gn, 0); if (familyinstance != null) { double panelArea = GetAreaOfTileInstance(familyinstance); // identify the panels drop in different ranges with different types if (panelArea > m_maxValue) { familyinstance.Symbol = fs_max; m_maxCounter++; } else if (panelArea < m_minValue) { familyinstance.Symbol = fs_min; m_minCounter++; } else { familyinstance.Symbol = fs_mid; m_okCounter++; } } } } }