// // (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.IO; using System.Xml; using System.Xml.Linq; using Autodesk.Revit.DB; using Autodesk.Revit.DB.PointClouds; using Autodesk.Revit.UI; namespace Revit.SDK.Samples.CS.PointCloudEngine { /// /// ExternalApplication used to register the point cloud engines managed by this sample. /// [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)] public class PointCloudTestApplication : IExternalApplication { #region IExternalApplication Members /// /// The implementation of IExternalApplication.OnStartup() /// /// The Revit application. /// Result.Succeeded public Result OnStartup(UIControlledApplication application) { try { // Register point cloud engines for the sample. // Predefined engine (non-randomized) IPointCloudEngine engine = new BasicPointCloudEngine(PointCloudEngineType.Predefined); PointCloudEngineRegistry.RegisterPointCloudEngine("apipc", engine, false); // Predefined engine with randomized points at the cell borders engine = new BasicPointCloudEngine(PointCloudEngineType.RandomizedPoints); PointCloudEngineRegistry.RegisterPointCloudEngine("apipcr", engine, false); // XML-based point cloud definition engine = new BasicPointCloudEngine(PointCloudEngineType.FileBased); PointCloudEngineRegistry.RegisterPointCloudEngine("xml", engine, true); // Create user interface for accessing predefined point clouds RibbonPanel panel = application.CreateRibbonPanel("Point cloud testing"); System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); panel.AddItem(new PushButtonData("AddPredefinedInstance", "Add predefined instance", assembly.Location, "Revit.SDK.Samples.CS.PointCloudEngine.AddPredefinedInstanceCommand")); panel.AddSeparator(); panel.AddItem(new PushButtonData("AddRandomizedInstance", "Add randomized instance", assembly.Location, "Revit.SDK.Samples.CS.PointCloudEngine.AddRandomizedInstanceCommand")); panel.AddSeparator(); panel.AddItem(new PushButtonData("AddTransformedInstance", "Add randomized instance\nat transform", assembly.Location, "Revit.SDK.Samples.CS.PointCloudEngine.AddTransformedInstanceCommand")); panel.AddSeparator(); panel.AddItem(new PushButtonData("SerializePointCloud", "Serialize point cloud (utility)", assembly.Location, "Revit.SDK.Samples.CS.PointCloudEngine.SerializePredefinedPointCloud")); } catch (Exception e) { TaskDialog.Show("Exception from OnStartup", e.ToString()); } return Result.Succeeded; } /// /// The implementation of IExternalApplication.OnShutdown() /// /// The Revit application. /// Result.Succeeded. public Result OnShutdown(UIControlledApplication application) { return Result.Succeeded; } #endregion } /// /// ExternalCommand to add a predefined point cloud. /// [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] public class AddPredefinedInstanceCommand : AddInstanceCommandBase, IExternalCommand { #region IExternalCommand Members /// /// The implementation for IExternalCommand.Execute() /// /// The Revit command data. /// The error message (ignored). /// The elements to display in the failure dialog (ignored). /// Result.Succeeded public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Document doc = commandData.View.Document; AddInstance(doc, "apipc", "", Transform.Identity); return Result.Succeeded; } #endregion } /// /// ExternalCommand to a predefined point cloud with randomized points. /// [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] public class AddRandomizedInstanceCommand : AddInstanceCommandBase, IExternalCommand { #region IExternalCommand Members /// /// The implementation for IExternalCommand.Execute() /// /// The Revit command data. /// The error message (ignored). /// The elements to display in the failure dialog (ignored). /// Result.Succeeded public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Document doc = commandData.View.Document; AddInstance(doc, "apipcr", "", Transform.Identity); return Result.Succeeded; } #endregion } /// /// ExternalCommand to add a predefined point cloud at a non-default transform. /// [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] public class AddTransformedInstanceCommand : AddInstanceCommandBase, IExternalCommand { #region IExternalCommand Members /// /// The implementation for IExternalCommand.Execute() /// /// The Revit command data. /// The error message (ignored). /// The elements to display in the failure dialog (ignored). /// Result.Succeeded public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Document doc = commandData.View.Document; Transform trf = Transform.CreateRotationAtPoint(XYZ.BasisZ, Math.PI / 6.0, new XYZ(10, 5, 0)); AddInstance(doc, "apipcr", "", trf); return Result.Succeeded; } #endregion } /// /// Base class for ExternalCommands used to add point cloud instances programmatically. /// public class AddInstanceCommandBase { /// /// Adds a point cloud instance programmatically. /// /// The document. /// The engine identifier string. /// The identifier for the particular point cloud. /// The transform to apply to the new point cloud instance. public void AddInstance(Document doc, String engineType, String identifier, Transform trf) { Transaction t = new Transaction(doc, "Create PC instance"); t.Start(); PointCloudType type = PointCloudType.Create(doc, engineType, identifier); PointCloudInstance.Create(doc, type.Id, trf); t.Commit(); } } /// /// Utility ExternalCommand to take a predefined point cloud and write the corresponding XML for it to disk. /// [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)] public class SerializePredefinedPointCloud : AddInstanceCommandBase, IExternalCommand { #region IExternalCommand Members /// /// The implementation for IExternalCommand.Execute() /// /// The Revit command data. /// The error message (ignored). /// The elements to display in the failure dialog (ignored). /// Result.Succeeded public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { PredefinedPointCloud cloud = new PredefinedPointCloud("dummy"); XDocument doc = new XDocument(); XElement root = new XElement("PointCloud"); cloud.SerializeObjectData(root); doc.Add(root); TextWriter writer = new StreamWriter(@"c:\serializedPC.xml"); doc.WriteTo(new XmlTextWriter(writer)); writer.Close(); return Result.Succeeded; } #endregion } }