Message 1 of 13
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Greeting to all, when switching from python to c# code I'm facing a NullReferenceException. In fact, SharpDevelop builds the script successfully and the exception comes up when hitting the run button in macro manager. So far I've identified that the exception triggers in the code that is wrapped inside the if ...NumberOfSegments>1 and if...NumberOfSegments==1:
Any thoughts on how to get this fixed?
Best regards
Transaction tr2=new Transaction(doc,"AFRY:Multi-Rebar Alignment");
tr2.Start();
view= doc.ActiveView;
cropbox=view.CropBox;
transform=cropbox.Transform;
XYZ bbMin=cropbox.Min;
XYZ bbMax=cropbox.Max;
Dictionary<int, Element> unTagged= new Dictionary<int, Element>(), rebTagged=new Dictionary<int, Element>();
Dictionary<int, XYZ> Top= new Dictionary<int, XYZ>(), Bottom=new Dictionary<int, XYZ>(), Right= new Dictionary<int, XYZ>(), Left=new Dictionary<int, XYZ>();
if (familyType=="Section")
{
for (int i = 0; i < rebars.Count; i++)
{
//a). rebar bounding box midpoint in 3d revit model system (it might retrieve a partial bounding box if the whole element is not visible)
Element reb=rebars[i];
if ((reb as Rebar).IsRebarFreeForm()== true)
{
unTagged.Add(i,reb);
}
else if ((reb as Rebar).IsRebarFreeForm()== false)
{
BoundingBoxXYZ rebBB=reb.get_BoundingBox(view);
XYZ midPoint=(rebBB.Max + rebBB.Min)/2;
//b). Transform the coord system into the local view coord system
Transform inverseTf=transform.Inverse;
XYZ rebLocInView=inverseTf.OfPoint(midPoint);
//c) Determine the center point of the cropview w.r.t view system coord (using z=0)
XYZ newMin=new XYZ(bbMin.X,bbMin.Y,0);
XYZ newMax=new XYZ(bbMax.X,bbMax.Y,0);
Curve line1=Line.CreateBound(newMax,newMin);
XYZ centerPt=line1.Evaluate(0.5,true);
//d) Get the direction of the distribution path from the rebar reinforcement
//dissclosure: Free form rebars do not support getshapedrivenaccessor
RebarShapeDrivenAccessor accessor=(reb as Rebar).GetShapeDrivenAccessor();
XYZ dir=accessor.GetDistributionPath().Direction;
//e).Get the curves of a typical rebar within the whole set
IList<Curve> curves=accessor.ComputeDrivingCurves();
//f). filter rebars that have more than 1 segment (Null Reference from what is inside segments>1 or ==1!!)
//try
//{
if ( ((doc.GetElement((reb as Rebar).GetShapeId()) as RebarShape).GetRebarShapeDefinition() as RebarShapeDefinitionBySegments).NumberOfSegments >1 )
{
//f.1) Stirrups that follows a horizontal path direction: set try & catch for diagonal stirrup that follow hz dir
if (Math.Round(Math.Abs(dir.X),4)== Math.Round(Math.Abs(view.RightDirection.X),4) && Math.Round(Math.Abs(dir.Y),4)==Math.Round(Math.Abs(view.RightDirection.Y),4))
{
rebTagged.Add(i,reb);
Left.Add(i,rebLocInView);
}
//f.2) Stirrups that follow a vertical path direction for rebars
if (Math.Round(Math.Abs(dir.Z),4)==Math.Round(Math.Abs(view.UpDirection.Z),4))
{
rebTagged.Add(i,reb);
Top.Add(i,rebLocInView);
}
}
if ( ((doc.GetElement((reb as Rebar).GetShapeId()) as RebarShape).GetRebarShapeDefinition() as RebarShapeDefinitionBySegments).NumberOfSegments == 1 )
{
//H.1).ANY REBAR ORIENTATION ALONG THE viewDirection: untagged()
if (Math.Round(Math.Abs(dir.X),4)== Math.Round(Math.Abs(view.ViewDirection.X),4) && (Math.Round(Math.Abs(dir.Y),4)==Math.Round(Math.Abs(view.UpDirection.Y),4)) )
{
unTagged.Add(i, reb);
}
//H.2) straight Horizontal rebar along viewUpDirection (points along UpDir as well) --> This goes at Top or bottom
if (Math.Round(Math.Abs(dir.X),4)== Math.Round(Math.Abs(view.UpDirection.X),4) && (Math.Round(Math.Abs(dir.Y),4)==Math.Round((Math.Abs(view.UpDirection.Y)),4)) && (Math.Round(Math.Abs((curves[0] as Line).Direction.Z)))==0 )
{
rebTagged.Add(i,reb);
Top.Add(i,rebLocInView);
}
//H.3) straight vertical rebal along viewRight direction path
if ( Math.Round(Math.Abs(dir.X),4) == Math.Round(Math.Abs(view.RightDirection.X),4) && Math.Round(Math.Abs(dir.Y),4) == Math.Round(Math.Abs(view.RightDirection.Y),4) && Math.Abs((curves[0] as Line).Direction.Z)==1)
{
rebTagged.Add(i,reb);
Left.Add(i,rebLocInView);
}
// H.4) Horizontal rebars as points along the right side
if (Math.Round(Math.Abs(dir.X),4) == Math.Round(Math.Abs(view.RightDirection.X),4) && Math.Round(Math.Abs(dir.Y),4) == Math.Round(Math.Abs(view.RightDirection.Y),4) && Math.Round(Math.Abs((curves[0] as Line).Direction.Z),4)==0 )
{
rebTagged.Add(i,reb);
Left.Add(i,rebLocInView);
}
}
//}
//catch (Exception)
//{
//unTagged.Add(i,reb);
//throw;
//}
}
}
}
tr2.Commit();
Solved! Go to Solution.