Importing ToolPalette and ToolPaletteGroup from xtp and xtg files require user interaction in AutoCAD. Here is a sample ObjectARX code to automate that. Some portion of this code retrieves information from the xtp / xtg files by accessing them as plain XML document. This code worked ok for simple xtp and xtg files that I used during my tests. It might however still need more error handling code which Iam sure you can include based on your needs.
CAcTcUiManager *pTcUiManager = AcTcUiGetManager();
CAcTcUiToolPaletteSet *pTpset = AcTcUiGetToolPaletteWindow();
// List of xtp files to import
AcArray<AcString> xtpFiles;
xtpFiles.append(ACRX_T("C:\\Temp\\MyTP.xtp"));
// List of xpg files to import
AcArray<AcString> xpgFiles;
xpgFiles.append(ACRX_T("C:\\Temp\\MyTPGroup.xpg"));
// Import the ToolPalette from the xtp file
// By default the toolpalette is added to the "AllPalettesGroup"
// After importing from the xtp file, get its name
// We will need the names to move the toolpalette to the
// toolpalette groups that will be imported from xpg file.
AcArray<AcString> toolPaletteNames;
for(int xtpFileIndex =0;
xtpFileIndex < xtpFiles.length(); xtpFileIndex++)
{
// Get the xtp file path
AcString xtpFilePath = xtpFiles.at(xtpFileIndex);
if(pTcUiManager->Import(xtpFilePath))
{
acutPrintf(ACRX_T("\nImported %s"), xtpFilePath);
}
// The ToolPalette was imported, but we need to find its
// name so we can relocate it to our ToolPaletteGroup
// Find the name of the ToolPalette that was now imported
// from the XTP file.
CComPtr<IXMLDOMDocument> pXTPDoc;
HRESULT hr = pXTPDoc.CoCreateInstance(CLSID_DOMDocument,
NULL, CLSCTX_INPROC_SERVER);
if (FAILED(hr))
return;
CComVariant xmlSource = xtpFilePath.constPtr();
VARIANT_BOOL varSuccess = VARIANT_FALSE;
hr = pXTPDoc->load(xmlSource, &varSuccess);
if (FAILED(hr) || ! varSuccess)
return;
// Find the "<Palette></Palette> nodes from the xml
CComPtr<IXMLDOMNodeList> pTPNodeList = NULL;
hr = pXTPDoc->getElementsByTagName(ACRX_T("Palette"),
&pTPNodeList);
if (FAILED(hr))
return;
long tpCount = 0;
hr = pTPNodeList->get_length(&tpCount);
if(FAILED(hr))
return;
pTPNodeList->reset();
for(int ii = 0; ii < tpCount; ii++)
{
CComPtr<IXMLDOMNode> pTPNode = NULL;
hr = pTPNodeList->get_item(ii, &pTPNode);
if(FAILED(hr))
return;
if(pTPNode)
{
CComPtr<IXMLDOMNode> pTPParentNode = NULL;
hr = pTPNode->get_parentNode(&pTPParentNode);
if(SUCCEEDED(hr))
{
// Check the node's parent name
// to be sure we can use it to find the
// ToolPalette name
CComBSTR bstrParentNodeName;
hr = pTPParentNode->get_nodeName
(&bstrParentNodeName);
if(SUCCEEDED(hr)
&& bstrParentNodeName == ACRX_T("ToolSource"))
{
BSTR bstrTPName = NULL;
hr = pTPNode->get_text(&bstrTPName);
if(SUCCEEDED(hr))
{
AcString toolPaletteName(bstrTPName);
toolPaletteNames.append(toolPaletteName);
::SysFreeString(bstrTPName);
}
}
}
}
}
}
// Import the ToolPaletteGroup from the xpg file
// By default the toolpalettegroup is empty
// After importing from the xpg file,
// We will move the toolpalettes that were imported to the
// toolpalette group that is imported from xpg file.
for(int xpgFileIndex =0;
xpgFileIndex < xpgFiles.length(); xpgFileIndex++)
{
AcString xpgFilePath = xpgFiles.at(xpgFileIndex);
// Create an XML Doc instance
CComPtr<IXMLDOMDocument> pXPGDoc;
HRESULT hr = pXPGDoc.CoCreateInstance(CLSID_DOMDocument,
NULL, CLSCTX_INPROC_SERVER);
if (FAILED(hr))
return;
// Load the XPG file as an XML doc
CComVariant xmlSource = xpgFilePath.constPtr();
VARIANT_BOOL bSuccess = VARIANT_FALSE;
hr = pXPGDoc->load(xmlSource, &bSuccess);
if (FAILED(hr) || ! bSuccess)
return;
// Get the XML nodes from the XPG file that corresponds to
// a ToolPalette Group
CComPtr<IXMLDOMNodeList> pTPGNodeList = NULL;
hr = pXPGDoc->getElementsByTagName(
ACRX_T("ToolPaletteGroup"),
&pTPGNodeList);
if (FAILED(hr))
return;
// Find the number of ToolPalette group nodes
// from the XPG file
long tpgCount = 0;
hr = pTPGNodeList->get_length(&tpgCount);
if(FAILED(hr))
return;
pTPGNodeList->reset();
for(int tpgIndex = 0; tpgIndex < tpgCount; tpgIndex++)
{
// Get a single XML node that corresponds
// to a ToolPalette Group
CComPtr<IXMLDOMNode> pTPGNode = NULL;
hr = pTPGNodeList->get_item(tpgIndex, &pTPGNode);
if(FAILED(hr) || pTPGNode == NULL)
return;
// Create a new ToolPalette group and
CAcTcUiToolPaletteGroup *pRootTpgroup
= pTpset->GetToolPaletteGroup(FALSE);
CAcTcUiToolPaletteGroup * pMyToolPaletteGroup = NULL;
pRootTpgroup->Clone(pMyToolPaletteGroup);
pMyToolPaletteGroup->Reset();
// Import the ToolPalette group settings
// from the XPG file
if(pMyToolPaletteGroup->Load(pTPGNode))
{
pRootTpgroup->AddItem(pMyToolPaletteGroup);
pTpset->SetActivePaletteGroup(pMyToolPaletteGroup);
acutPrintf(ACRX_T("\nImported %s"), xpgFilePath);
}
// Add the custom ToolPalettes
// to the custom ToolPalette group.
CAcTcUiToolPaletteGroup *pAllPalettesGroup
= pTpset->GetAllPalettesGroup();
for(int ii =0; ii < toolPaletteNames.length(); ii++)
{
AcString toolPaletteName = toolPaletteNames.at(ii);
CAcTcUiToolPalette *pMyCustomPalette
= pAllPalettesGroup->FindPalette(
toolPaletteName, NULL);
if(pMyCustomPalette != NULL)
{
// Add the custom ToolPalette to the
// custom ToolPalette group.
pMyToolPaletteGroup->AddItem(pMyCustomPalette);
}
}
}
}