OpenCASCADE Application Framework Data Framework Services
Posted on 2012-11-06 21:40 eryar 閱讀(3039) 評論(7) 編輯 收藏 引用 所屬分類: 2.OpenCASCADEOpenCASCADE Application Framework
Data Framework Services
一、概述Overview
OpenCASCADE的數據框架對來自不同程序的數據提供了統一的處理環境。這就簡化了數據交換、修改,也保證了數據統一性、穩定性。實現方法需要用到以下部分:
u 標號Tha tag
u 標簽The label
u 屬性The attribute
Figure 1. Contents of a document
如上圖所示,框架樹的第一個標簽(label)是根標簽(root)。每個標簽(label)有個以整數表示的標號(tag)。由當前標簽的標號到根標簽的標號,可以得到一個惟一的標號列表,如:0:1:2:1。
每個標簽(label)可以一些屬性(attribute),這些屬性可以包含數據。每個屬性由GUID來區分。標簽最重要的性質是其入口只是數據框架的一個地址。
二、標號The Tag
一個標號Tag就是一個整數,它用兩種方式標示了一個label:
u 相對標示法 Relative identification:一個標簽的標號只與其父標簽有關系。如對于一個指定的標簽,可能由四個子標簽組成,其標號分別為2,7,18,100。使用相對標示方法,在設置屬性時有安全的范圍。
u 絕對標示法 Absolute identification:一個標簽在數據框架中位置由無歧義的、從根標簽的標號到當前標簽的標號用冒號表示的標號列表(list of tags)來表示。
不管采用哪種方法,都要注意的是這些標號的值沒什么實際的意義。只是用來確定每個標簽在樹結構的位置,都是為了使用文檔支持Undo/Redo的功能。
創建標號Tag的兩種方式:
u Random delivery 隨機創建;
u User-defined delivery 用戶自定義創建;
正如字面所說,隨機創建標號時,標號是由系統隨機生成。用戶自定義創建標號時,標號的值是創建標號函數的參數。
1. 隨機創建標號法生成子標簽 Creating child labels using random delivery of tags
使用TDF_TagSource::NewChild來添加標簽。如下代碼所示,函數NewChild的參數level2也是一個TDF_Label。
2. 用戶自定義創建標號法生成子標簽 Creation of a child label by user delivery from a tag
創建子標簽的另一種方式就是用戶自定義創建。即在指定標號創建標簽。可以使用TDF_Label::FindChild和TDF_Label::Tag來獲得指定標號的子標簽。
如上代碼所示,3是需要查找的標簽的標號,Standard_False用來表示若查找不到指定標號時是否創建子標簽。
三、標簽The Label
標號(Tag)給了標簽(Label)一個唯一的地址。數據框架中的標簽是包含屬性,綁定數據的容器。數據框架的本質是一個標簽樹,如下圖所示:
數據框架中的標簽不能被刪除,因此,當文檔打開后已經的數據框架結構不能被刪除。
1. 創建標簽 Label creation
可以在任意層次創建標簽,也可以找到標簽在數據框架中的深度(Depth)。TDF_Label提供上述功能。
2. 創建子標簽 Creating child labels
在數據框架中指定標簽上創建子標簽使用TDF_Label::FindChild。如下所示:
當把FindChilde的第二個參數設為Standard_True時,就確保了查找不到指定標號的標簽時會創建一個標簽。如下所示:
3. 訪問子標簽 Retrieving child labels
可以使用遍歷器來訪問當前標簽的第一層的子標簽。如下所示:
也可以訪問當前標簽的所有子標簽,如下所示:
使用TDF_Tool::Entry可以得到當前標簽的入口字符串,如下所示:
4. 訪問父標簽
訪問當前標簽的父標簽:
四、屬性The Attribute
標簽本身不包含任何數據。所有數據,不管什么類型,程序的非程序的數據都是保存在屬性中。屬性是綁定在標簽上,且屬性可以是任意類型的數據。OCAF提供許多直接可以使用的屬性如:整數、實數、軸、平面。也有用于拓樸、功能、可視化的屬性。每種類型的屬性由GUID來標識。這樣做的好處就是所有類型的屬性都以相同的方式處理。可以創建新的實例,訪問、綁定到標簽和從標簽上刪除等。
1.訪問標簽的屬性
使用函數TDF_Label::FindAttribute來訪問標簽的屬性。如下例所示,
2.使用GUID來標識屬性 Identifying an attribute using a GUID
可以創建一個屬性對象并得到其GUID。如下例所示,創建了一個整數屬性,通過方法ID來得到GUID。
3. 將屬性綁定到標簽 Attaching an attribute to a label
使用函數TDF_Label::Add來將屬性綁定到標簽。重復綁定相同GUID的屬性到一個標簽會出現錯誤。TDF_Attribute::Label可以得到綁定屬性的標簽。如下所示:
4. 測試標簽綁定狀態 Testing the attachment to a label
可以使用函數TDF_Attribute::IsA來檢驗屬性是否已經綁定到標簽上,函數的參數是屬性的GUID。在下例所示,是檢測當前標簽是否有整數屬性,然后得出這個標簽屬性的數量。函數TDF_Tool::HasAttribute用來檢測標簽是否綁定的有屬性,函數TDF_Tool::NbAttributes返回標簽綁定屬性的數量。
5. 刪除標簽的屬性 Removing an attribute from a label
若要將屬性從標簽中刪除,可以使用TDF_Label::Forget,函數參數為屬性的GUID。若要刪除標簽所有屬性,使用函數TDF_Label::ForgetAll。
6. 特定屬性的創建 Specific attribute creation
見《Application Framework User's Guide》。
五、示例程序 Sample Code
1: //------------------------------------------------------------------------------
2: // Copyright (c) 2012 eryar All Rights Reserved.
3: //
4: // File : Main.cpp
5: // Author : eryar@163.com
6: // Date : 2012-11-4 21:25
7: // Version : 0.1v
8: //
9: // Description : OpenCASCADE Application Framework sample code.
10: //
11: //==============================================================================
12:13: #include <iostream>14: using namespace std;15:16: #include <TDF_Tool.hxx>17: #include <TDF_ChildIterator.hxx>18: #include <TDataStd_Integer.hxx>19: #include <TDocStd_Document.hxx>20:21: // Use Toolkit: TKLCAF
22: #pragma comment(lib, "TKernel.lib")
23: #pragma comment(lib, "TKLCAF.lib")
24:25: int main(int argc, char* argv[])26: {27: TCollection_AsciiString entry;28:29: Handle_TDocStd_Document myDF = new TDocStd_Document("myDocument");30:31: // Main label and root label of the data framework.
32: TDF_Label mainLabel = myDF->Main();33: TDF_Label root = mainLabel.Root();34:35: cout<<"Main label :";
36: mainLabel.EntryDump(cout);37:38: cout<<endl<<"Root label :";
39: root.EntryDump(cout);40:41: // Create a label with tag 10 at Root.
42: TDF_Label myLabel = root.FindChild(10);43:44: cout<<endl<<"Entry of the new label :";
45: myLabel.EntryDump(cout);46:47: // Retrieving child labels.
48: cout<<endl<<"Retrieving child labels: "<<endl;
49: for (TDF_ChildIterator it(root); it.More(); it.Next())
50: {51: it.Value().EntryDump(cout);52: cout<<endl;53: }54:55: // Attaching an attribute to a label.
56: Handle_TDataStd_Integer INT = new TDataStd_Integer;
57: myLabel.AddAttribute(INT);58:59: // Testing of attribute attachment.
60: if (myLabel.IsAttribute(INT->GetID()))
61: {62: cout<<"The attribute is attached to the label."<<endl;
63: }64: else
65: {66: cout<<"The attribute is not attached to the label."<<endl;
67: }68:69: // Removing an attribute from a label.
70: myLabel.ForgetAttribute(INT->GetID());71:72: // Testing of attribute attachment.
73: if (myLabel.IsAttribute(INT->GetID()))
74: {75: cout<<"The attribute is attached to the label."<<endl;
76: }77: else
78: {79: cout<<"The attribute is not attached to the label."<<endl;
80: }81:82: return 0;
83: }
輸出結果如下所示:
1: Main label :0:12: Root label :0:3: Entry of the new label :0:10
4: Retrieving child labels:5: 0:16: 0:107: The attribute is attached to the label.8: The attribute is not attached to the label.
9: Press any key to continue . . .
2012-11-06