My code don't work

My code don't work

arnaudgenty
Contributor Contributor
671 Views
4 Replies
Message 1 of 5

My code don't work

arnaudgenty
Contributor
Contributor

 

Hi

I start on the API. I want to draw a line from my walls, then make a dimension. I don't have error message, but my lines are never drawn.

Where is my mistake?
Thank you

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Structure;

namespace Mypluggin
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class Class1 : IExternalCommand
    {
        ExternalCommandData m_commandData = null;    //store external command
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
             m_commandData = commandData;
            //Get application and documnet objects
            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;

         try
            {
                IList<Reference> pickedRef = null;
                ElementArray elem = new ElementArray();

                Selection sel = uiapp.ActiveUIDocument.Selection;
                // classe WallPickFilter qui filtre la sélection des murs
                WallPickFilter selFilter = new WallPickFilter();

                // pickedRef = liste des murs sélectionnés
                pickedRef = sel.PickObjects(ObjectType.Element, selFilter, "Please select walls");

                // nombre de murs sélectionnés
                int Num_Wall = pickedRef.Count();
                for (int i = 0; i < Num_Wall; i++)
                {
                    Transaction trans = new Transaction(m_commandData.Application.ActiveUIDocument.Document, "Add Line");
                    trans.Start();
                    Reference picked = null;
                    picked = pickedRef[i];
                    Element eleme = doc.GetElement(picked);
                    
                    AddLine(eleme, picked);
                    trans.Commit();
                }   
            }
            // If the user right clicks or presses Echap, handle the expression catch 
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return Result.Cancelled;
            }

            // Catch other errors
            catch (Exception ex)
            {
                message = ex.Message;
               
            }

            return Result.Succeeded;
        }


        public class WallPickFilter : ISelectionFilter
        {
            public bool AllowElement(Element e)
            {
                return (e.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_Walls));

            }
            public bool AllowReference(Reference r, XYZ p)
            {
                return false;
            }
        }

        public bool AddLine(Element elem, Reference picked)
        {
            Autodesk.Revit.UI.UIApplication app = m_commandData.Application;
            Document doc = app.ActiveUIDocument.Document;   
            Location location = elem.Location;
            //get location curve
            LocationCurve locationline = location as LocationCurve;
            Line line = locationline.Curve as Line;
            //New Line
            XYZ LocalocationEndPoint = new XYZ();
            LocalocationEndPoint = locationline.Curve.GetEndPoint(1);
            XYZ LocalocationStartPoint = new XYZ();
            LocalocationStartPoint = locationline.Curve.GetEndPoint(0);
            Line newLine = null;
            newLine = Line.CreateBound(LocalocationStartPoint, LocalocationEndPoint);
            return true;
        }   
    }
}

 

 

 

 

0 Likes
672 Views
4 Replies
Replies (4)
Message 2 of 5

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @arnaudgenty ,

In AddLine method,just add the following code to create a line in the document

Line newLine = null;
                newLine = Line.CreateBound(LocalocationStartPoint, LocalocationEndPoint);
                XYZ origin = new XYZ(0, 0, 0);
                XYZ normal = new XYZ(0, 10, 0);
                Plane p = Plane.CreateByNormalAndOrigin(normal, origin);
                SketchPlane sp = SketchPlane.Create(doc, p);
                ModelLine ML = doc.Create.NewModelCurve(newLine, sp) as ModelLine;

If this helped solve your problem please mark it as solution, so other users can get this solutions as wellSmiley Happy


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 5

arnaudgenty
Contributor
Contributor

Thank you for your answer.

With your code, I have this message :

 

"Curve must be in the plane

Parameter name: pCurveCopyA transaction or sub-transaction was opened but not closed. All changes to the active document made by External Command will be discarded."

 

 Transaction trans = new Transaction(doc, "Add Line");
            trans.Start();

            Location location = elem.Location;

            //get location curve
            LocationCurve locationline = location as LocationCurve;
            Line line = locationline.Curve as Line;
            //New Line

            XYZ LocalocationEndPoint = new XYZ();
            LocalocationEndPoint = line.GetEndPoint(1);
            XYZ LocalocationStartPoint = new XYZ();
            LocalocationStartPoint = line.GetEndPoint(0);
            Line newLine = Line.CreateBound(LocalocationStartPoint, LocalocationEndPoint);
            XYZ origin = new XYZ (0, 0, 0);
            XYZ normal = new XYZ(0, 10, 0);
            Plane p = Plane.CreateByNormalAndOrigin(normal,origin);
            SketchPlane sp = SketchPlane.Create(doc, p);
            ModelLine ML = doc.Create.NewModelCurve(newLine, sp) as ModelLine;

            trans.Commit();
            
            return true;
0 Likes
Message 4 of 5

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @arnaudgenty ,

I think the problem lies with the normal of the plane

XYZ normal = new XYZ(1, 1, 0);

I hope this helps

 

 

 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 5

arnaudgenty
Contributor
Contributor

There was still a problem. In fact it was necessary:

XYZ normal = new XYZ(0, 0, 1);

 

the created curve is at the axis. How to say that we want the outer or inner curve of the wall?
I searched on BuildingCoder but I found nothing (or understood ...)

0 Likes