Exemplo n.º 1
0
        /// <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(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                m_application = commandData.Application;
                m_document    = m_application.ActiveUIDocument;
                if (m_document.Document.IsFamilyDocument)
                {
                    m_CreationBase = m_document.Document.FamilyCreate;
                }
                else
                {
                    m_CreationBase = m_document.Document.Create;
                }

                //Pick a face from UI, create a new sketch plane via the face and set it to the current view.
                Reference      faceRef         = m_document.Selection.PickObject(ObjectType.Face, new PlanarFaceFilter(m_document.Document), "Please pick a planar face to set the work plane. ESC for cancel.");
                GeometryObject geoObject       = m_document.Document.GetElement(faceRef).GetGeometryObjectFromReference(faceRef);
                PlanarFace     planarFace      = geoObject as PlanarFace;
                SketchPlane    faceSketchPlane = CreateSketchPlane(planarFace.FaceNormal, planarFace.Origin);
                if (faceSketchPlane != null)
                {
                    Transaction changeSketchPlane = new Transaction(m_document.Document, "Change Sketch Plane.");
                    changeSketchPlane.Start();
                    m_document.Document.ActiveView.SketchPlane = faceSketchPlane;
                    m_document.Document.ActiveView.ShowActiveWorkPlane();
                    changeSketchPlane.Commit();
                }

                // Pick point from current work plane with snaps.
                ObjectSnapTypes snapType = ObjectSnapTypes.Centers | ObjectSnapTypes.Endpoints | ObjectSnapTypes.Intersections
                                           | ObjectSnapTypes.Midpoints | ObjectSnapTypes.Nearest | ObjectSnapTypes.WorkPlaneGrid;
                XYZ point = m_document.Selection.PickPoint(snapType, "Please pick a point to place component.");

                // Create a model curve by a circle with picked point as center.
                Transaction createModelCurve = new Transaction(m_document.Document, "Create a circle.");
                createModelCurve.Start();
                Curve circle = Arc.Create(point, 5, 0, Math.PI * 2, faceSketchPlane.GetPlane().XVec, faceSketchPlane.GetPlane().YVec);
                m_CreationBase.NewModelCurve(circle, faceSketchPlane);
                createModelCurve.Commit();

                return(Result.Succeeded);
            }
            catch (Exceptions.OperationCanceledException)
            {
                // Selection Cancelled. For picking face and picking point.
                return(Result.Cancelled);
            }
            catch (System.Exception ex)
            {
                // If any error, give error information and return failed
                message = ex.Message;
                return(Result.Failed);
            }
        }
Exemplo n.º 2
0
        /// <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(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                m_application = commandData.Application;
                m_document = m_application.ActiveUIDocument;
                if (m_document.Document.IsFamilyDocument)
                    m_CreationBase = m_document.Document.FamilyCreate;
                else
                    m_CreationBase = m_document.Document.Create;

                //Pick a face from UI, create a new sketch plane via the face and set it to the current view.
                Reference faceRef = m_document.Selection.PickObject(ObjectType.Face, new PlanarFaceFilter(m_document.Document), "Please pick a planar face to set the work plane. ESC for cancel.");
                GeometryObject geoObject = m_document.Document.GetElement(faceRef).GetGeometryObjectFromReference(faceRef);
                PlanarFace planarFace = geoObject as PlanarFace;
                SketchPlane faceSketchPlane = CreateSketchPlane(planarFace.Normal, planarFace.Origin);
                if (faceSketchPlane != null)
                {
                    Transaction changeSketchPlane = new Transaction(m_document.Document, "Change Sketch Plane.");
                    changeSketchPlane.Start();
                    m_document.Document.ActiveView.SketchPlane = faceSketchPlane;
                    m_document.Document.ActiveView.ShowActiveWorkPlane();
                    changeSketchPlane.Commit();
                }

                // Pick point from current work plane with snaps.
                ObjectSnapTypes snapType = ObjectSnapTypes.Centers | ObjectSnapTypes.Endpoints | ObjectSnapTypes.Intersections
                | ObjectSnapTypes.Midpoints | ObjectSnapTypes.Nearest | ObjectSnapTypes.WorkPlaneGrid;
                XYZ point = m_document.Selection.PickPoint(snapType, "Please pick a point to place component.");

                // Create a model curve by a circle with picked point as center.
                Transaction createModelCurve = new Transaction(m_document.Document, "Create a circle.");
                createModelCurve.Start();
                Curve circle = m_application.Application.Create.NewArc(point, 5, 0, Math.PI * 2, faceSketchPlane.Plane.XVec, faceSketchPlane.Plane.YVec);
                m_CreationBase.NewModelCurve(circle, faceSketchPlane);
                createModelCurve.Commit();

                return Result.Succeeded;
            }
            catch (Exceptions.OperationCanceledException)
            {
                // Selection Cancelled. For picking face and picking point.
                return Result.Cancelled;
            }
            catch (System.Exception ex)
            {
                // If any error, give error information and return failed
                message = ex.Message;
                return Result.Failed;
            }
        }