// // (C) Copyright 2003-2019 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Analysis; namespace Revit.SDK.Samples.GeometryCreation_BooleanOperation.CS { class AnalysisVisualizationFramework { /// /// The singleton instance of AnalysisVisualizationFramework /// private static AnalysisVisualizationFramework Instance; /// /// revit document /// private Autodesk.Revit.DB.Document m_doc; /// /// The created view list /// private List viewNameList = new List(); /// /// The ID of schema which SpatialFieldManager register /// private static int SchemaId = -1; /// /// Constructor /// /// Revit document private AnalysisVisualizationFramework(Autodesk.Revit.DB.Document doc) { m_doc = doc; } /// /// Get the singleton instance of AnalysisVisualizationFramework /// /// Revit document /// The singleton instance of AnalysisVisualizationFramework public static AnalysisVisualizationFramework getInstance(Autodesk.Revit.DB.Document doc) { if (Instance == null) { Instance = new AnalysisVisualizationFramework(doc); } return Instance; } /// /// Paint a solid in a new named view /// /// solid /// Given the name of view public void PaintSolid(Solid s, String viewName) { View view; if (!viewNameList.Contains(viewName)) { IList viewFamilyTypes = new FilteredElementCollector(m_doc).OfClass(typeof(ViewFamilyType)).ToElements(); ElementId View3DId = ElementId.InvalidElementId; foreach (Element e in viewFamilyTypes) { if (e.Name == "3D View") { View3DId = e.Id; } } //view = m_doc.Create.NewView3D(new XYZ(1, 1, 1)); view = View3D.CreateIsometric(m_doc, View3DId); ViewOrientation3D viewOrientation3D = new ViewOrientation3D(new XYZ(1, -1, -1), new XYZ(1, 1, 1), new XYZ(1, 1, -2)); (view as View3D).SetOrientation(viewOrientation3D); (view as View3D).SaveOrientation(); view.Name = viewName; viewNameList.Add(viewName); } else { view = (((new FilteredElementCollector(m_doc). OfClass(typeof(View))).Cast()). Where(e => e.Name == viewName)).First(); } SpatialFieldManager sfm = SpatialFieldManager.GetSpatialFieldManager(view); if (sfm == null) sfm = SpatialFieldManager.CreateSpatialFieldManager(view, 1); if (SchemaId != -1) { IList results = sfm.GetRegisteredResults(); if (!results.Contains(SchemaId)) { SchemaId = -1; } } if (SchemaId == -1) { AnalysisResultSchema resultSchema1 = new AnalysisResultSchema("PaintedSolid" + viewName, "Description"); AnalysisDisplayStyle displayStyle = AnalysisDisplayStyle.CreateAnalysisDisplayStyle( m_doc, "Real_Color_Surface" + viewName, new AnalysisDisplayColoredSurfaceSettings(), new AnalysisDisplayColorSettings(), new AnalysisDisplayLegendSettings()); resultSchema1.AnalysisDisplayStyleId = displayStyle.Id; SchemaId = sfm.RegisterResult(resultSchema1); } FaceArray faces = s.Faces; Transform trf = Transform.Identity; foreach (Face face in faces) { int idx = sfm.AddSpatialFieldPrimitive(face, trf); IList uvPts = null; IList valList = null; ComputeValueAtPointForFace(face, out uvPts, out valList, 1); FieldDomainPointsByUV pnts = new FieldDomainPointsByUV(uvPts); FieldValues vals = new FieldValues(valList); sfm.UpdateSpatialFieldPrimitive(idx, pnts, vals, SchemaId); } } /// /// Compute the value of face on specific point /// /// /// /// /// private static void ComputeValueAtPointForFace(Face face, out IList uvPts, out IList valList, int measurementNo) { List doubleList = new List(); uvPts = new List(); valList = new List(); BoundingBoxUV bb = face.GetBoundingBox(); for (double u = bb.Min.U; u < bb.Max.U + 0.0000001; u = u + (bb.Max.U - bb.Min.U) / 1) { for (double v = bb.Min.V; v < bb.Max.V + 0.0000001; v = v + (bb.Max.V - bb.Min.V) / 1) { UV uvPnt = new UV(u, v); uvPts.Add(uvPnt); XYZ faceXYZ = face.Evaluate(uvPnt); // Specify three values for each point for (int ii = 1; ii <= measurementNo; ii++) doubleList.Add(faceXYZ.DistanceTo(XYZ.Zero) * ii); valList.Add(new ValueAtPoint(doubleList)); doubleList.Clear(); } } } } }