Unable to generate rebar (Revit 2024)

Unable to generate rebar (Revit 2024)

h-tsukamotoZ6HPG
Contributor Contributor
436 Views
4 Replies
Message 1 of 5

Unable to generate rebar (Revit 2024)

h-tsukamotoZ6HPG
Contributor
Contributor

Hello,

I am attempting to generate a 3D rebar model in Revit 2024 by coding with the help of some sample codes. I'm starting by placing a single beam and then generating one rebar hosted by that beam, but it's not working at all. The error message I receive is "System.InvalidCastException" (within ○○.dll). Below, I am sharing the code I wrote(sorry for vb.net). I would appreciate it if you could point out any mistakes. Thank you for your assistance.

 

Using tran As New Transaction(ActiveDoc, "Rebartest")
tran.Start("Rebartest")
Try


For Each elms As Element In GIR

' Create a RebarShape object
Dim rebarShape As RebarShape = Nothing ' Obtain the RebarShape from document

' Create a RebarBarType object
Dim rebarBarType As RebarBarType = New FilteredElementCollector(ActiveDoc).OfClass(GetType(RebarBarType)).Cast(Of RebarBarType)().FirstOrDefault(Function(type) type.Name = "D35")

' Create a RebarBarType object
Dim hookType As FilteredElementCollector = New FilteredElementCollector(ActiveDoc).OfClass(GetType(RebarHookType))
Dim firstHookType As RebarHookType = hookType.Cast(Of RebarHookType)().FirstOrDefault()
' Define the rebar distribution path
Dim curves As New List(Of Curve)()
' Add curves to define the path of the rebar
Dim length As Integer = 70
Dim startPoint As New XYZ(0, 0, 0)
Dim endPoint As New XYZ(length, 0, 0)

' ジオメトリオプションを設定
Dim geomOptions As New Options()
geomOptions.ComputeReferences = True
geomOptions.DetailLevel = ViewDetailLevel.Fine

' 要素からジオメトリを取得
Dim geometryElement As GeometryElement = elms.Geometry(geomOptions)
If geometryElement Is Nothing Then
TaskDialog.Show("Error", "No geometry found for the element.")
Return
End If

' Create a line between the two points
' ジオメトリ要素内をループしてエッジを抽出
For Each geomObj As GeometryObject In geometryElement
If TypeOf geomObj Is GeometryInstance Then
Dim geomInstance As GeometryInstance = DirectCast(geomObj, GeometryInstance)
Dim instanceGeometry As GeometryElement = geomInstance.GetInstanceGeometry()
For Each instObj As GeometryObject In instanceGeometry
If TypeOf instObj Is Solid Then
Dim solid As Solid = DirectCast(instObj, Solid)
For Each edge As Edge In solid.Edges
curves.Add(edge.AsCurve())
Next
End If
Next
ElseIf TypeOf geomObj Is Solid Then
Dim solid As Solid = DirectCast(geomObj, Solid)
For Each edge As Edge In solid.Edges
curves.Add(edge.AsCurve())
Next
End If
Next

Dim firstCurve As Curve = curves.First()

' Create the rebar
Dim recre As [Structure].Rebar = [Structure].Rebar.CreateFromCurves(
ActiveDoc,
autodesk.Revit.DB.Structure.RebarStyle.Standard,
rebarBarType,
firstHookType,
firstHookType,
elms,
New XYZ(1, 1, 0),
firstCurve,
autodesk.Revit.DB.Structure.RebarHookOrientation.Left,
autodesk.Revit.DB.Structure.RebarHookOrientation.Right,
True,
True
)

' Set additional parameters or constraints if needed
Next

tran.Commit()
Catch ex As Exception

tran.RollBack()
End Try

End Using

0 Likes
Accepted solutions (1)
437 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

The exception that you mention is thrown by one specific line of code causing the error. It is hard for me to say what line of code that might be just by looking at it. I am not a computer, after all. I suggest that you launch your external command inside a debugging environment that enables you to step through the code line by line and observe how it affects the program variables and other effects. In the debugger, you will be able to see directly which line of code is causing the error and why. Before that, I would also suggest reading about the importance of debugging:

  

https://duckduckgo.com/?q=importance+of+debugging+in+programming

    

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 5

h-tsukamotoZ6HPG
Contributor
Contributor

Dear Jeremy,

Nice to meet you, and thank you for your response. I apologize for any inconvenience caused by the unclear points in my initial question. I am continuously debugging, and the point where the exception is being bypassed is as follows:

Rebar_error2.PNG

The issue occurs at the point where the CreateFromCurves function is used. As shown in the watch expression below, although the values are being passed in, the result is "nothing."

Rebar_error.PNG

 

Continuing with the code as it is will result in an exception being thrown. If there is any additional information needed to solve the problem, please let me know. I would appreciate your guidance. Thank you for your assistance.

0 Likes
Message 4 of 5

longt61
Advocate
Advocate
Accepted solution

I think the problem is you using "firstCurve" instead of "curves" in your method. You can take a look at the documentation and see that the method receives "a list of curves" instead of "a single curve". That is why Revit api tried to cast your "firstCurve" to a list and failed. Hence, the exception.

You can tried it again passing "curves" into the method, or passing a new list that contains only "firstCurve". I think it should work given all your other inputs are valid.

0 Likes
Message 5 of 5

h-tsukamotoZ6HPG
Contributor
Contributor

Dear longt61

Thank you for your response. By focusing on the part you mentioned and making the necessary corrections, I was able to successfully generate the rebar model! I'm still considering how to define the centerline of the rebar, but it looks like I can move on to the next phase. Thank you very much.

0 Likes