その名前が示すように、このユーティリティ クラスは外部ファイル参照に関する情報を提供します。ExternalFileUtils.GetAllExternalFileReferences()メソッドは、ドキュメント内の外部ファイル参照となるすべての要素の ElementIds のコレクションを返します(ネストされた Revit リンクの ID は返さず、最上位の参照のみを返す点に注意してください)。このユーティリティ クラスには、IsExternalFileReference()と GetExternalFileReference()の 2 つのメソッドがあります。これらは Element クラスの同様の名前のメソッドと同じ機能を持ちますが、最初に取得した Element ではなく ElementId を持っている場合に使用できます。
TransmissionDataTransmissionData は、外部ファイル参照の前の状態とリクエストされた状態の両方に関する情報を確認します。つまり、この TransmissionData のドキュメントが最後に開かれたときの参照のロード状態とパスが格納されることを意味します。また、ドキュメントが次に開かれたときに Revit が実行する操作に関するロード状態とパス情報も格納します。
このため、TransmissionData を使用すると、関連付けられている Revit ドキュメント全体を開くことなしに、外部ファイル参照に操作を実行できます。ReadTransmissionData や WriteTransmissionData メソッドを使用すると、外部参照に関する情報を取得したり、その情報を変更できます。たとえば、参照がすべて LinkedFileStatus.Unloaded に設定されている TransmissionData オブジェクトで WriteTransmissionData を呼び出すと、次にドキュメントを開いたときに参照がロードされない原因となる場合があります。
TransmissionData は外部ファイルに参照を追加、削除できません。外部ファイル参照の要素に対応していない ElementId を使用して AddExternalFileReference を呼び出すと、ファイル ロード時に情報が無視されます。
次の例では、特定の場所のファイルの TransmissionData を読み取り、次にドキュメントが開かれたときに Revit リンクをすべてロード解除するように設定します。
|
コード領域: Revit リンクをロード解除 |
void UnloadRevitLinks(ModelPath location)
/// This method will set all Revit links to be unloaded the next time the document at the given location is opened.
/// The TransmissionData for a given document only contains top-level Revit links, not nested links.
/// However, nested links will be unloaded if their parent links are unloaded, so this function only needs to look at the document's immediate links.
{
// access transmission data in the given Revit file
TransmissionData transData = TransmissionData.ReadTransmissionData(location);
if (transData != null)
{
// collect all (immediate) external references in the model
ICollection<ElementId> externalReferences = transData.GetAllExternalFileReferenceIds();
// find every reference that is a link
foreach (ElementId refId in externalReferences)
{
ExternalFileReference extRef = transData.GetLastSavedReferenceData(refId);
if (extRef.ExternalFileReferenceType == ExternalFileReferenceType.RevitLink)
{
// we do not want to change neither the path nor the path-type
// we only want the links to be unloaded (shouldLoad = false)
transData.SetDesiredReferenceData(refId, extRef.GetPath(), extRef.PathType, false);
}
}
// make sure the IsTransmitted property is set
transData.IsTransmitted = true;
// modified transmission data must be saved back to the model
TransmissionData.WriteTransmissionData(location, transData);
}
else
{
Autodesk.Revit.UI.TaskDialog.Show("Unload Links", "The document does not have any transmission data");
}
}
|
TransmissionData オブジェクトを読み取るには、静的メソッド TransmissionData.ReadTransmissionData を呼び出す必要があります。これには ModelPath オブジェクトが必要です。
中央ファイルを参照する ModelPath オブジェクトを作成する方法が 2 つあります。1 つ目は、ModelPathUtils とベース ModelPath クラスの使用を含む方法です。手順は次のとおりです。
次の例は、中央ファイル testmodel.rvt が Revit Server の root フォルダ SHACNG035WQRP にあると想定してこのメソッドを表しています。
|
コード領域: ModelPath を使用して中央ファイルへのパスを構築 |
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class RevitCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Transaction trans = new Transaction(doc, "ExComm");
trans.Start();
string visiblePath = ModelPathUtils.GetRevitServerPrefix() + "/testmodel.rvt";
ModelPath serverPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(visiblePath);
TransmissionData transData = TransmissionData.ReadTransmissionData(serverPath);
string mymessage = null;
if (transData != null)
{
//access the data in the transData here.
}
else
{
Autodesk.Revit.UI.TaskDialog.Show("Unload Links",
"The document does not have any transmission data");
}
trans.Commit();
return Result.Succeeded;
}
}
|
中央ファイルを参照する ModelPath オブジェクトを構築する 2 番目の方法は、子クラス ServerPath を使用する方法です。プログラムがローカルのサーバ名を知っている場合にこの方法を使用できますが、Revit UI から Revit ユーザがサーバ名を変更する可能性があるため推奨されません。手順は次のとおりです。
new ServerPath(“ServerNameOrServerIp”, “relative path without the initial forward slash”).
以下のコードはこのメソッドを表しています。
|
コード領域: ServerPath を使用して中央ファイルへのパスを構築 |
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class RevitCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Transaction trans = new Transaction(doc, "ExComm");
trans.Start();
ServerPath serverPath = new ServerPath("SHACNG035WQRP", "testmodel.rvt");
TransmissionData transData = TransmissionData.ReadTransmissionData(serverPath);
string mymessage = null;
if (transData != null)
{
//access the data in the transData here.
}
else
{
Autodesk.Revit.UI.TaskDialog.Show("Unload Links",
"The document does not have any transmission data");
}
trans.Commit();
return Result.Succeeded;
}
}
|