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.
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.
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:
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.
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:
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.
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;
}
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;
}
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.
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.
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?
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?
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 !
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 !
Can you step through and debug?
Can you step through and debug?
Can't find what you're looking for? Ask the community or share your knowledge.