Creating multiple XCAFDoc_DocumentTool in single TDocStd_Document

Hi,
I am working on a project which has multiple modules dealing with different aspects of geometry generation.
We are using one TDocStd_Document per study in the project. Study means an opened case which can have more than one above mentioned modules.
And we are using separate dedicated label from TDocStd_Document to each module. Like (0:1, 0:2, 0:3...)
Is it possible to initialize XCAFDoc_DocumentTool attribute in each of those labels?
Or is there any other way I can use XCAF structure initialized different labels that I can use independently?

Thanks in advance.
Vinojan T.

Dmitrii Pasukhin's picture

Hello,

It is possible. But I cannot guarantee that it will work successfully in all user cases.

XCAFDoc_DocumentTool - it is a just attribute. You can put it whenever you want. By default it put on first child of root. The code is opensource, that is why, you can see creation stage (XCAFDoc_DocumentTool::Set). We interested in method XCAFDoc_DocumentTool::DocLabel, that return created first child or find existed).

To put this attribute on 0:2 or other label (before it was only 0:1). You need to use the next code:

//=======================================================================
//function : SetDocumentTool
//purpose  : 
//=======================================================================
Handle(XCAFDoc_DocumentTool) SetDocumentTool(const Handle(TDocStd_Document)& theDoc,
                                             const Standard_Integer theLevel = 1 /*0:1 in the graph*/)
{
  if (theDoc.IsNull() || theLevel < 1)
  {
    return NULL;
  }
  Handle(XCAFDoc_DocumentTool) anAttr;
  const TDF_Label aRootL = theDoc->Main().Root();
  TDF_Label aDocL = aRootL.FindChild(theLevel);
  if (!aDocL.FindAttribute (XCAFDoc_DocumentTool::GetID(), anAttr)) {
    anAttr = new XCAFDoc_DocumentTool;
    aDocL.AddAttribute(anAttr);
    anAttr->Init();
    // setting all nodes and sub labels what you want
  }
  return anAttr;
}

Unfortunatelly,  I think you will need to rework all XCAF tools to work with you code. But if you upgrade it, working with manylavel document attribute will be easy.

The other best way to configure working document tool - is refence on root. you can validate this reference externally. For example, you can create child 0:3 and create reference on it (document tool attribute will be created automatically). Then all tools and all alghoritms recognize 0:3 as a  valid document tool.

Code sample to create reference:

//=======================================================================
//function : Set
//purpose  : 
//=======================================================================
Handle(XCAFDoc_DocumentTool) SetReferenceToWorkingDocTool(const Handle(TDocStd_Document)& theDoc,
                                                          const Standard_Integer theLevel = 1 /*0 in the graph*/)
{
  if (theDoc.IsNull() || theLevel < 1)
  {
    return NULL;
  }
  TDF_Label aRootL = theDoc->Main().Root();
  TDF_Label aDocL = aRootL.FindChild(theLevel);
  static Standard_GUID aDocumentToolRefID("efd212eb-6dfd-11d4-b9c8-0060b0ee281b");
  Handle(TDataStd_TreeNode) aRootNodeNew = TDataStd_TreeNode::Set(aRootL, aDocumentToolRefID);
  Handle(TDataStd_TreeNode) aLNode = TDataStd_TreeNode::Set(aDocL, aDocumentToolRefID);
  aLNode->SetFather(aRootNodeNew);
  aRootNodeNew->SetFirst(aLNode);
  return XCAFDoc_DocumentTool::Set(aDocL);
} 

If you need more information, please let me know.

Best regards, Dmitrii.

Dmitrii Pasukhin's picture

I really recommend to use the second sample. It very helpful for lesson workspace. Using second sample you can choose active document, create new. All algorithm will be work successfully.

The first sample just to view for setting attribute.

Best regards, Dmitrii.

Vinojan Thiyagarasah's picture

Hi Dmitrii, Thank you for your quick response and it is working well with the second sample you mentioned. Now, I can initialize multiple XCAFDoc_DocumentTool attribute in different TDF_Label s of same TDocStd_Document. Then when I am switching between modules, I have to set the root node and document tool label relations as in the attached image.

Thank you again Dmitrii.

Vinojan T.

Attachments: