OpenCASCADE Application Framework
Data Framework Services
eryar@163.com
一、概述Overview
OpenCASCADE的數(shù)據(jù)框架對來自不同程序的數(shù)據(jù)提供了統(tǒng)一的處理環(huán)境。這就簡化了數(shù)據(jù)交換、修改,也保證了數(shù)據(jù)統(tǒng)一性、穩(wěn)定性。實(shí)現(xiàn)方法需要用到以下部分:
u 標(biāo)號Tha tag
u 標(biāo)簽The label
u 屬性The attribute
Figure 1. Contents of a document
如上圖所示,框架樹的第一個(gè)標(biāo)簽(label)是根標(biāo)簽(root)。每個(gè)標(biāo)簽(label)有個(gè)以整數(shù)表示的標(biāo)號(tag)。由當(dāng)前標(biāo)簽的標(biāo)號到根標(biāo)簽的標(biāo)號,可以得到一個(gè)惟一的標(biāo)號列表,如:0:1:2:1。
每個(gè)標(biāo)簽(label)可以一些屬性(attribute),這些屬性可以包含數(shù)據(jù)。每個(gè)屬性由GUID來區(qū)分。標(biāo)簽最重要的性質(zhì)是其入口只是數(shù)據(jù)框架的一個(gè)地址。
二、標(biāo)號The Tag
一個(gè)標(biāo)號Tag就是一個(gè)整數(shù),它用兩種方式標(biāo)示了一個(gè)label:
u 相對標(biāo)示法 Relative identification:一個(gè)標(biāo)簽的標(biāo)號只與其父標(biāo)簽有關(guān)系。如對于一個(gè)指定的標(biāo)簽,可能由四個(gè)子標(biāo)簽組成,其標(biāo)號分別為2,7,18,100。使用相對標(biāo)示方法,在設(shè)置屬性時(shí)有安全的范圍。
u 絕對標(biāo)示法 Absolute identification:一個(gè)標(biāo)簽在數(shù)據(jù)框架中位置由無歧義的、從根標(biāo)簽的標(biāo)號到當(dāng)前標(biāo)簽的標(biāo)號用冒號表示的標(biāo)號列表(list of tags)來表示。
不管采用哪種方法,都要注意的是這些標(biāo)號的值沒什么實(shí)際的意義。只是用來確定每個(gè)標(biāo)簽在樹結(jié)構(gòu)的位置,都是為了使用文檔支持Undo/Redo的功能。
創(chuàng)建標(biāo)號Tag的兩種方式:
u Random delivery 隨機(jī)創(chuàng)建;
u User-defined delivery 用戶自定義創(chuàng)建;
正如字面所說,隨機(jī)創(chuàng)建標(biāo)號時(shí),標(biāo)號是由系統(tǒng)隨機(jī)生成。用戶自定義創(chuàng)建標(biāo)號時(shí),標(biāo)號的值是創(chuàng)建標(biāo)號函數(shù)的參數(shù)。
1. 隨機(jī)創(chuàng)建標(biāo)號法生成子標(biāo)簽 Creating child labels using random delivery of tags
使用TDF_TagSource::NewChild來添加標(biāo)簽。如下代碼所示,函數(shù)NewChild的參數(shù)level2也是一個(gè)TDF_Label。
2. 用戶自定義創(chuàng)建標(biāo)號法生成子標(biāo)簽 Creation of a child label by user delivery from a tag
創(chuàng)建子標(biāo)簽的另一種方式就是用戶自定義創(chuàng)建。即在指定標(biāo)號創(chuàng)建標(biāo)簽。可以使用TDF_Label::FindChild和TDF_Label::Tag來獲得指定標(biāo)號的子標(biāo)簽。
如上代碼所示,3是需要查找的標(biāo)簽的標(biāo)號,Standard_False用來表示若查找不到指定標(biāo)號時(shí)是否創(chuàng)建子標(biāo)簽。
三、標(biāo)簽The Label
標(biāo)號(Tag)給了標(biāo)簽(Label)一個(gè)唯一的地址。數(shù)據(jù)框架中的標(biāo)簽是包含屬性,綁定數(shù)據(jù)的容器。數(shù)據(jù)框架的本質(zhì)是一個(gè)標(biāo)簽樹,如下圖所示:
數(shù)據(jù)框架中的標(biāo)簽不能被刪除,因此,當(dāng)文檔打開后已經(jīng)的數(shù)據(jù)框架結(jié)構(gòu)不能被刪除。
1. 創(chuàng)建標(biāo)簽 Label creation
可以在任意層次創(chuàng)建標(biāo)簽,也可以找到標(biāo)簽在數(shù)據(jù)框架中的深度(Depth)。TDF_Label提供上述功能。
2. 創(chuàng)建子標(biāo)簽 Creating child labels
在數(shù)據(jù)框架中指定標(biāo)簽上創(chuàng)建子標(biāo)簽使用TDF_Label::FindChild。如下所示:
當(dāng)把FindChilde的第二個(gè)參數(shù)設(shè)為Standard_True時(shí),就確保了查找不到指定標(biāo)號的標(biāo)簽時(shí)會創(chuàng)建一個(gè)標(biāo)簽。如下所示:
3. 訪問子標(biāo)簽 Retrieving child labels
可以使用遍歷器來訪問當(dāng)前標(biāo)簽的第一層的子標(biāo)簽。如下所示:
也可以訪問當(dāng)前標(biāo)簽的所有子標(biāo)簽,如下所示:
使用TDF_Tool::Entry可以得到當(dāng)前標(biāo)簽的入口字符串,如下所示:
4. 訪問父標(biāo)簽
訪問當(dāng)前標(biāo)簽的父標(biāo)簽:
四、屬性The Attribute
標(biāo)簽本身不包含任何數(shù)據(jù)。所有數(shù)據(jù),不管什么類型,程序的非程序的數(shù)據(jù)都是保存在屬性中。屬性是綁定在標(biāo)簽上,且屬性可以是任意類型的數(shù)據(jù)。OCAF提供許多直接可以使用的屬性如:整數(shù)、實(shí)數(shù)、軸、平面。也有用于拓樸、功能、可視化的屬性。每種類型的屬性由GUID來標(biāo)識。這樣做的好處就是所有類型的屬性都以相同的方式處理。可以創(chuàng)建新的實(shí)例,訪問、綁定到標(biāo)簽和從標(biāo)簽上刪除等。
1.訪問標(biāo)簽的屬性
使用函數(shù)TDF_Label::FindAttribute來訪問標(biāo)簽的屬性。如下例所示,
2.使用GUID來標(biāo)識屬性 Identifying an attribute using a GUID
可以創(chuàng)建一個(gè)屬性對象并得到其GUID。如下例所示,創(chuàng)建了一個(gè)整數(shù)屬性,通過方法ID來得到GUID。
3. 將屬性綁定到標(biāo)簽 Attaching an attribute to a label
使用函數(shù)TDF_Label::Add來將屬性綁定到標(biāo)簽。重復(fù)綁定相同GUID的屬性到一個(gè)標(biāo)簽會出現(xiàn)錯(cuò)誤。TDF_Attribute::Label可以得到綁定屬性的標(biāo)簽。如下所示:
4. 測試標(biāo)簽綁定狀態(tài) Testing the attachment to a label
可以使用函數(shù)TDF_Attribute::IsA來檢驗(yàn)屬性是否已經(jīng)綁定到標(biāo)簽上,函數(shù)的參數(shù)是屬性的GUID。在下例所示,是檢測當(dāng)前標(biāo)簽是否有整數(shù)屬性,然后得出這個(gè)標(biāo)簽屬性的數(shù)量。函數(shù)TDF_Tool::HasAttribute用來檢測標(biāo)簽是否綁定的有屬性,函數(shù)TDF_Tool::NbAttributes返回標(biāo)簽綁定屬性的數(shù)量。
5. 刪除標(biāo)簽的屬性 Removing an attribute from a label
若要將屬性從標(biāo)簽中刪除,可以使用TDF_Label::Forget,函數(shù)參數(shù)為屬性的GUID。若要?jiǎng)h除標(biāo)簽所有屬性,使用函數(shù)TDF_Label::ForgetAll。
6. 特定屬性的創(chuàng)建 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: }
輸出結(jié)果如下所示:
1: Main label :0:1
2: Root label :0:
3: Entry of the new label :0:10
4: Retrieving child labels:
5: 0:1
6: 0:10
7: The attribute is attached to the label.
8: The attribute is not attached to the label.
9: Press any key to continue . . .
eryar@163.com
2012-11-06