Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rebar selected column

6 REPLIES 6
Reply
Message 1 of 7
youssefGC
381 Views, 6 Replies

Rebar selected column

youssefGC
Advocate
Advocate

Hello forum.

I'm trying to code a program in order to rebar a selected column from the user using "Create From Curves", but i found a problem, when I execute the code an error message was shown :

 

"Référence d'objet n'est pas définie a une instance d'un objet"

 

The RebarBarType argument was extracted from a selected rebar


Source Code :

try
            {
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                Document doc = uidoc.Document; ;

                //select the column
                Reference reff = uidoc.Selection.PickObject(ObjectType.Element);
                ElementId idd = reff.ElementId;
                FamilyInstance P1 = doc.GetElement(idd) as FamilyInstance;

                //select the rebar
                Reference reff2 = uidoc.Selection.PickObject(ObjectType.Element);
                ElementId idd2 = reff2.ElementId;
                Rebar P2 = doc.GetElement(idd2) as Rebar;
                //get the rebar bar type 
                RebarBarType RT = P2.Document.GetElement(P2.GetTypeId()) as RebarBarType;
                

                using (Transaction trns = new Transaction(doc,"Rebar"))
                {
                    trns.Start();

                    LocationPoint location = P1.Location as LocationPoint;
                    XYZ origin = location.Point;
                    XYZ normal = new XYZ(1, 0, 0);
                    XYZ rebarLineEnd = new XYZ(origin.X, origin.Y, origin.Z + 2);
                    Line rebarLine = Line.CreateBound(origin, rebarLineEnd);
                    IList<Curve> curves = new List<Curve>();
                    curves.Add(rebarLine);

                    Rebar rebar = Rebar.CreateFromCurves(doc, Autodesk.Revit.DB.Structure.RebarStyle.Standard, RT, null, null,
                                      P1, normal, curves, RebarHookOrientation.Right, RebarHookOrientation.Left, true, true);
                    
                     trns.Commit();
                      }

                      return Result.Succeeded;
                    }
                    catch (Exception ex)
                    {
                        message = ex.Message;
                        TaskDialog.Show("error",message);
                        return Result.Failed;
                    }

But i can't locate the source of the problem

 

Thank you.

Rebar selected column

Hello forum.

I'm trying to code a program in order to rebar a selected column from the user using "Create From Curves", but i found a problem, when I execute the code an error message was shown :

 

"Référence d'objet n'est pas définie a une instance d'un objet"

 

The RebarBarType argument was extracted from a selected rebar


Source Code :

try
            {
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                Document doc = uidoc.Document; ;

                //select the column
                Reference reff = uidoc.Selection.PickObject(ObjectType.Element);
                ElementId idd = reff.ElementId;
                FamilyInstance P1 = doc.GetElement(idd) as FamilyInstance;

                //select the rebar
                Reference reff2 = uidoc.Selection.PickObject(ObjectType.Element);
                ElementId idd2 = reff2.ElementId;
                Rebar P2 = doc.GetElement(idd2) as Rebar;
                //get the rebar bar type 
                RebarBarType RT = P2.Document.GetElement(P2.GetTypeId()) as RebarBarType;
                

                using (Transaction trns = new Transaction(doc,"Rebar"))
                {
                    trns.Start();

                    LocationPoint location = P1.Location as LocationPoint;
                    XYZ origin = location.Point;
                    XYZ normal = new XYZ(1, 0, 0);
                    XYZ rebarLineEnd = new XYZ(origin.X, origin.Y, origin.Z + 2);
                    Line rebarLine = Line.CreateBound(origin, rebarLineEnd);
                    IList<Curve> curves = new List<Curve>();
                    curves.Add(rebarLine);

                    Rebar rebar = Rebar.CreateFromCurves(doc, Autodesk.Revit.DB.Structure.RebarStyle.Standard, RT, null, null,
                                      P1, normal, curves, RebarHookOrientation.Right, RebarHookOrientation.Left, true, true);
                    
                     trns.Commit();
                      }

                      return Result.Succeeded;
                    }
                    catch (Exception ex)
                    {
                        message = ex.Message;
                        TaskDialog.Show("error",message);
                        return Result.Failed;
                    }

But i can't locate the source of the problem

 

Thank you.

6 REPLIES 6
Message 2 of 7
jeremy_tammik
in reply to: youssefGC

jeremy_tammik
Autodesk
Autodesk

Happy New Year!

 

Before addressing your question, some advice on programming in general: I suggest you always clearly separate the UI from the calculation, and the calculation from the model modification. So:

  

  • Put the call to PickObject in its own separate exception handler (since an exception is thrown if the user cancels the selection)
  • Perform the calculation step to define the list of curves
  • Start the transaction and perform the rebar creation

  

Secondly, I would suggest adding a selection filter derived from the ISelectionFilter interface to your pick method:

  

  

That will guarantee that the selected object really is what you expect, and simplify the picking for the user.

  

Now, to address your question: maybe the user picked the wrong object. Maybe P@ or RT end up null. Debug through your code step by step interactively in the debugger and you will see which statement is causing the error. Then it will become obvious how it can be fixed.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open

Happy New Year!

 

Before addressing your question, some advice on programming in general: I suggest you always clearly separate the UI from the calculation, and the calculation from the model modification. So:

  

  • Put the call to PickObject in its own separate exception handler (since an exception is thrown if the user cancels the selection)
  • Perform the calculation step to define the list of curves
  • Start the transaction and perform the rebar creation

  

Secondly, I would suggest adding a selection filter derived from the ISelectionFilter interface to your pick method:

  

  

That will guarantee that the selected object really is what you expect, and simplify the picking for the user.

  

Now, to address your question: maybe the user picked the wrong object. Maybe P@ or RT end up null. Debug through your code step by step interactively in the debugger and you will see which statement is causing the error. Then it will become obvious how it can be fixed.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 7
youssefGC
in reply to: jeremy_tammik

youssefGC
Advocate
Advocate

Thank you for the answer.

I try to use a selection filter of selection. And to make sure, I put a message box if the Rebar Type found or not.

but the same error.

 

try
            {
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                Document doc = uidoc.Document; ;
                RebarBarType RT = null;

                Reference reff = uidoc.Selection.PickObject(ObjectType.Element);
                ElementId idd = reff.ElementId;
                FamilyInstance P1 = doc.GetElement(idd) as FamilyInstance;

                var all_rebar_types =new FilteredElementCollector(doc)
                    .OfCategory(BuiltInCategory.OST_Rebar).WhereElementIsElementType()
                    .ToElements();
                
                foreach (var rebar_type in all_rebar_types)
                {
                    string rebar_name = rebar_type.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM).AsString();
                    if (rebar_name == "01")
                    {
                        RT = rebar_type as RebarBarType;
                        TaskDialog.Show("Message", "01 Exist");
                        break;
                    }
                        
                }

                using (Transaction trns = new Transaction(doc,"Rebar"))
                {
                    trns.Start();

                    LocationPoint location = P1.Location as LocationPoint;
                    XYZ origin = location.Point;
                    XYZ normal = new XYZ(1, 0, 0);
                    XYZ rebarLineEnd = new XYZ(origin.X, origin.Y, origin.Z + 2);
                    Line rebarLine = Line.CreateBound(origin, rebarLineEnd);
                    IList<Curve> curves = new List<Curve>();
                    curves.Add(rebarLine);


                    Rebar rebar = Rebar.CreateFromCurves(doc, Autodesk.Revit.DB.Structure.RebarStyle.Standard, RT, null, null,
                                      P1, normal, curves, RebarHookOrientation.Right, RebarHookOrientation.Left, true, true);
                    

                trns.Commit();
                }
                return Result.Succeeded;
             }
             catch (Exception ex)
            {
            message = ex.Message;
            TaskDialog.Show("error",message);
            return Result.Failed;
            }
0 Likes

Thank you for the answer.

I try to use a selection filter of selection. And to make sure, I put a message box if the Rebar Type found or not.

but the same error.

 

try
            {
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                Document doc = uidoc.Document; ;
                RebarBarType RT = null;

                Reference reff = uidoc.Selection.PickObject(ObjectType.Element);
                ElementId idd = reff.ElementId;
                FamilyInstance P1 = doc.GetElement(idd) as FamilyInstance;

                var all_rebar_types =new FilteredElementCollector(doc)
                    .OfCategory(BuiltInCategory.OST_Rebar).WhereElementIsElementType()
                    .ToElements();
                
                foreach (var rebar_type in all_rebar_types)
                {
                    string rebar_name = rebar_type.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM).AsString();
                    if (rebar_name == "01")
                    {
                        RT = rebar_type as RebarBarType;
                        TaskDialog.Show("Message", "01 Exist");
                        break;
                    }
                        
                }

                using (Transaction trns = new Transaction(doc,"Rebar"))
                {
                    trns.Start();

                    LocationPoint location = P1.Location as LocationPoint;
                    XYZ origin = location.Point;
                    XYZ normal = new XYZ(1, 0, 0);
                    XYZ rebarLineEnd = new XYZ(origin.X, origin.Y, origin.Z + 2);
                    Line rebarLine = Line.CreateBound(origin, rebarLineEnd);
                    IList<Curve> curves = new List<Curve>();
                    curves.Add(rebarLine);


                    Rebar rebar = Rebar.CreateFromCurves(doc, Autodesk.Revit.DB.Structure.RebarStyle.Standard, RT, null, null,
                                      P1, normal, curves, RebarHookOrientation.Right, RebarHookOrientation.Left, true, true);
                    

                trns.Commit();
                }
                return Result.Succeeded;
             }
             catch (Exception ex)
            {
            message = ex.Message;
            TaskDialog.Show("error",message);
            return Result.Failed;
            }
Message 4 of 7
jeremy_tammik
in reply to: youssefGC

jeremy_tammik
Autodesk
Autodesk

Oh dear, how sad. Happy New Year anyway!

  

I searched The Building Coder for CreateFromCurves and found: 

  

  

The first links to a blog post by Aaron Lu. That and the other article explain the details of using the method and provide several examples of doing so. I hope that helps you further.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open

Oh dear, how sad. Happy New Year anyway!

  

I searched The Building Coder for CreateFromCurves and found: 

  

  

The first links to a blog post by Aaron Lu. That and the other article explain the details of using the method and provide several examples of doing so. I hope that helps you further.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 5 of 7
mhannonQ65N2
in reply to: youssefGC

mhannonQ65N2
Advocate
Advocate

Is the column Slanted? The cast on line 31 will return null for slanted columns, causing an error on line 32. Can you find which line the error is on?

0 Likes

Is the column Slanted? The cast on line 31 will return null for slanted columns, causing an error on line 32. Can you find which line the error is on?

Message 6 of 7
youssefGC
in reply to: mhannonQ65N2

youssefGC
Advocate
Advocate

In my case, I didn't find any problem in selection, but the source of the problem is the variable RT (RebarBarType).

Till today, i can't locate the problem exactly !

0 Likes

In my case, I didn't find any problem in selection, but the source of the problem is the variable RT (RebarBarType).

Till today, i can't locate the problem exactly !

Message 7 of 7
jeremy_tammik
in reply to: youssefGC

jeremy_tammik
Autodesk
Autodesk

Can you step through and debug?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open

Can you step through and debug?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report