- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Solved! Go to Solution.