Golfstream3912
2007-04-21, 03:22 PM
That it is necessary to change in the text of the program that it was loaded group instead of family?
The set example loads family.
Imports System
Imports System.IO
Imports Autodesk
Imports Autodesk.Revit
Imports Autodesk.Revit.Application
Imports Autodesk.Revit.Elements
Imports Autodesk.Revit.Geometry
'
' LoadFamily - loads a family of the given family name.
' '
Public Class RvtCmd_LoadFamily
Implements Revit.IExternalCommand
Public Function Execute(ByVal commandData As Autodesk.Revit.ExternalCommandData, ByRef message As String, ByVal elements As Revit.ElementSet) As Revit.IExternalCommand.Result Implements Revit.IExternalCommand.Execute
Dim rvtApp As Autodesk.Revit.Application = commandData.Application
' family file name. hard coded for simplicity.
'
Dim fileName As String = "Copier04.rfa"
' (1) hard coded family path.
' (2) or search a family path in the library paths.
Dim filePath As String = findFile(rvtApp, fileName)
If (filePath Is Nothing) Then
MsgBox("cannot find the file " + fileName)
Return IExternalCommand.Result.Failed
End If
' set a full path to call LoadFamily method.
Dim fullPath As String = filePath + "\" + fileName
If (rvtApp.ActiveDocument.LoadFamily(fullPath)) Then
MsgBox("loaded a family ok.")
Return Revit.IExternalCommand.Result.Succeeded
End If
MsgBox("failed to load a family.")
'Return IExternalCommand.Result.Failed ' bug in Revit 8.0
Return IExternalCommand.Result.Succeeded
End Function
' Helper function - find the given file name in the set of Revit library paths.
'
Public Function findFile(ByVal rvtApp As Revit.Application, ByVal fileName As String) As String
Dim paths As Autodesk.Revit.Collections.StringStringMap = rvtApp.Options.LibraryPaths
Dim iter As Autodesk.Revit.Collections.StringStringMapIterator = paths.ForwardIterator
' loop through each path in the collection.
Do While (iter.MoveNext())
Dim path As String = iter.Current
Dim filePath As String = SearchFile(path, fileName)
If Not (filePath Is Nothing) Then
Return filePath
End If
Loop
Return Nothing
End Function
' Helper function - recursively search the given file name under the current directory.
'
Public Function SearchFile(ByVal path As String, ByVal fileName As String) As String
' search this directory
Dim fname As String
For Each fname In Directory.GetFiles(path, fileName)
MsgBox("I found the file in: " + fname)
Return path
Next
' recursively search child directories.
Dim dname As String
For Each dname In Directory.GetDirectories(path)
Dim filePath As String = SearchFile(dname, fileName)
If Not (filePath Is Nothing) Then
Return filePath
End If
Next
Return Nothing
End Function
End Class
The set example loads family.
Imports System
Imports System.IO
Imports Autodesk
Imports Autodesk.Revit
Imports Autodesk.Revit.Application
Imports Autodesk.Revit.Elements
Imports Autodesk.Revit.Geometry
'
' LoadFamily - loads a family of the given family name.
' '
Public Class RvtCmd_LoadFamily
Implements Revit.IExternalCommand
Public Function Execute(ByVal commandData As Autodesk.Revit.ExternalCommandData, ByRef message As String, ByVal elements As Revit.ElementSet) As Revit.IExternalCommand.Result Implements Revit.IExternalCommand.Execute
Dim rvtApp As Autodesk.Revit.Application = commandData.Application
' family file name. hard coded for simplicity.
'
Dim fileName As String = "Copier04.rfa"
' (1) hard coded family path.
' (2) or search a family path in the library paths.
Dim filePath As String = findFile(rvtApp, fileName)
If (filePath Is Nothing) Then
MsgBox("cannot find the file " + fileName)
Return IExternalCommand.Result.Failed
End If
' set a full path to call LoadFamily method.
Dim fullPath As String = filePath + "\" + fileName
If (rvtApp.ActiveDocument.LoadFamily(fullPath)) Then
MsgBox("loaded a family ok.")
Return Revit.IExternalCommand.Result.Succeeded
End If
MsgBox("failed to load a family.")
'Return IExternalCommand.Result.Failed ' bug in Revit 8.0
Return IExternalCommand.Result.Succeeded
End Function
' Helper function - find the given file name in the set of Revit library paths.
'
Public Function findFile(ByVal rvtApp As Revit.Application, ByVal fileName As String) As String
Dim paths As Autodesk.Revit.Collections.StringStringMap = rvtApp.Options.LibraryPaths
Dim iter As Autodesk.Revit.Collections.StringStringMapIterator = paths.ForwardIterator
' loop through each path in the collection.
Do While (iter.MoveNext())
Dim path As String = iter.Current
Dim filePath As String = SearchFile(path, fileName)
If Not (filePath Is Nothing) Then
Return filePath
End If
Loop
Return Nothing
End Function
' Helper function - recursively search the given file name under the current directory.
'
Public Function SearchFile(ByVal path As String, ByVal fileName As String) As String
' search this directory
Dim fname As String
For Each fname In Directory.GetFiles(path, fileName)
MsgBox("I found the file in: " + fname)
Return path
Next
' recursively search child directories.
Dim dname As String
For Each dname In Directory.GetDirectories(path)
Dim filePath As String = SearchFile(dname, fileName)
If Not (filePath Is Nothing) Then
Return filePath
End If
Next
Return Nothing
End Function
End Class