// // (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. // namespace Revit.SDK.Samples.ObjectViewer.CS { using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using Autodesk.Revit; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.DB.Structure; using Element = Autodesk.Revit.DB.Element; using GeometryElement = Autodesk.Revit.DB.GeometryElement; /// /// main command class of the ObjectViewer application /// public class ObjectViewer { /// /// choose the display kinds of preview /// public enum DisplayKinds { /// /// Geometry Model /// GeometryModel, /// /// AnalyticalModel /// AnalyticalModel } private Element m_selected; // selected element to display private View m_currentView; // current View for preview // current detail level for preview private ViewDetailLevel m_detailLevel = ViewDetailLevel.Fine; private List m_allViews = new List(); // all Views in current project // current display kind for preview private DisplayKinds m_displayKind = DisplayKinds.GeometryModel; // selected element's parameters' data private SortableBindingList m_paras; // current sketch instance to draw the element private Sketch3D m_currentSketch3D = null; // indicate whether select view or detail private bool m_isSelectView = true; /// /// Current display kind for preview /// public DisplayKinds DisplayKind { get { return m_displayKind; } set { m_displayKind = value; UpdateSketch3D(); } } /// /// Current View for preview /// public View CurrentView { get { return m_currentView; } set { m_currentView = value; m_isSelectView = true; UpdateSketch3D(); } } /// /// All Views in current project /// public ReadOnlyCollection AllViews { get { return new ReadOnlyCollection(m_allViews); } } /// /// Current detail level for preview /// public ViewDetailLevel DetailLevel { get { return m_detailLevel; } set { m_detailLevel = value; m_isSelectView = false; UpdateSketch3D(); } } /// /// Current sketch instance to draw the element /// public Sketch3D CurrentSketch3D { get { return m_currentSketch3D; } } /// /// Selected element's parameters' data /// public SortableBindingList Params { get { return m_paras; } } /// /// constructor /// public ObjectViewer() { UIDocument doc = Command.CommandData.Application.ActiveUIDocument; ElementSet selection = new ElementSet(); foreach (ElementId elementId in doc.Selection.GetElementIds()) { selection.Insert(doc.Document.GetElement(elementId)); } // only one element should be selected if (0 == selection.Size) { throw new ErrorMessageException("Please select an element."); } if (1 < selection.Size) { throw new ErrorMessageException("Please select only one element."); } // get selected element foreach (Element e in selection) { m_selected = e; } // get current view and all views m_currentView = doc.Document.ActiveView; FilteredElementIterator itor = (new FilteredElementCollector(doc.Document)).OfClass(typeof(View)).GetElementIterator(); itor.Reset(); while (itor.MoveNext()) { View view = itor.Current as View; // Skip view templates because they're invisible in project browser, invalid for geometry elements if (null != view && !view.IsTemplate) { m_allViews.Add(view); } } // create a instance of Sketch3D GeometryData geomFactory = new GeometryData(m_selected, m_currentView); m_currentSketch3D = new Sketch3D(geomFactory.Data3D, Graphics2DData.Empty); //get a instance of ParametersFactory and then use it to create Parameters ParasFactory parasFactory = new ParasFactory(m_selected); m_paras = parasFactory.CreateParas(); } /// /// update current Sketch3D using current UCS and settings /// private void UpdateSketch3D() { if (m_displayKind == DisplayKinds.GeometryModel) { GeometryData geomFactory; if(m_isSelectView) { geomFactory = new GeometryData(m_selected, m_currentView); m_detailLevel = ViewDetailLevel.Undefined; } else { geomFactory = new GeometryData(m_selected, m_detailLevel, m_currentView); } Graphics3DData geom3DData = geomFactory.Data3D; Graphics3DData old3DData = m_currentSketch3D.Data3D; geom3DData.CurrentUCS = old3DData.CurrentUCS; m_currentSketch3D.Data3D = geom3DData; m_currentSketch3D.Data2D = Graphics2DData.Empty; } else if (m_displayKind == DisplayKinds.AnalyticalModel) { ModelData modelFactory = new ModelData(m_selected); Graphics3DData model3DData = modelFactory.Data3D; Graphics2DData model2DData = modelFactory.Data2D; Graphics3DData old3DData = m_currentSketch3D.Data3D; model3DData.CurrentUCS = old3DData.CurrentUCS; m_currentSketch3D.Data3D = model3DData; m_currentSketch3D.Data2D = model2DData; } } } }