06-20-2020, 11:32 AM
I wonder if something has changed in CORE 0.7.4 because now if I get CADtools settings with this code, my plugin crashes on quit (a NULL address) sometime after the plugin's shutdown function. I only know that the error happens with CADtools 11 on Mac AI 2019, since that's the only CADtools install I have. Also, HDI_CORE_ILLUSTRATOR->allPlugins() takes about 40 seconds to run! I'll share my code in case it's useful to someone else.
Here's a struct in globals to store the CT settings:
Here's the function to retrieve the CADtools settings:
Also note that the strings for scale name, from units, and to units are only returned for custom user scales, not for the built in architectural or engineering scales. I hope it's helpful for others. Comments, of course, are welcome.
Here's a struct in globals to store the CT settings:
Code:
struct ctSettingsStruct{
bool enabled; // does the user choose to access CADtools settings
std::string scaleName;
double scaler;
double fromValue;
std::string fromUnits;
std::string relationship;
double toValue;
std::string toUnits;
std::string axoName;
double rotation;
double tilt;
double angleA;
double angleB;
bool foreshorten;
double xScaler, yScaler, zScaler;
};
ctSettingsStruct ctSettings;
Here's the function to retrieve the CADtools settings:
Code:
bool axo::Plugin::getCTdata(){
if (HDI_CORE_ILLUSTRATOR->documentCount() == 0){
return false;
}
if (!globals.ctSettings.enabled)
return false;
bool gotIt = false;
long ctVers(0); // for storing CADtools version no.
this->CTlabel.setText("Finding CADtools plugin...");
this->CTdlg.show();
this->CTdlg.update();
// note: the floating dialog CTdlg does not display :-/
// Some notice for the user would be helpful because the next line takes about 40 seconds to run!
hdi::core::Illustrator::ThirdPartyPluginMap otherPlugins;
otherPlugins = HDI_CORE_ILLUSTRATOR->allPlugins();
hdi::core::Illustrator::ThirdPartyPluginMap::iterator it = otherPlugins.find("Hot Door CADtools");
if (it == otherPlugins.end())
it = otherPlugins.find("HDICADtools");
if (it == otherPlugins.end()){
globals.ctSettings.enabled = false;
hdi::core::alerts::error("CADtools plugin cannot be found");
this->CTdlg.hide();
return false;
}
hdi::core::ThirdPartyPlugin* CTpi = it->second;
this->CTlabel.setText("Connecting with CADtools...");
hdi::ct::PublicMsgGetVersionData* ctVersData = (hdi::ct::PublicMsgGetVersionData*) HDI_CORE_ILLUSTRATOR->alloc(sizeof(hdi::ct::PublicMsgGetVersionData));
ctVersData->d.self = HDI_CORE_PLUGIN->spPluginRef();
SPMessageData* SPdata = reinterpret_cast<SPMessageData*>(ctVersData);
SPErr SPmsgErr, SPresult;
bool OKsoFar = CTpi->prepMessageData(*SPdata);
if (OKsoFar){
SPmsgErr = CTpi->message(hdi::ct::publicMsgCADtoolsCaller, hdi::ct::publicMsgGetVersionSelector, SPdata, SPresult);
if (!SPmsgErr && !SPresult && ctVersData->success){
gotIt = true;
ctVers = (ctVersData->major*10000) + (ctVersData->minor * 100) + ctVersData->revision;
gotIt = (ctVers >= 110102);
}
else{
gotIt = false;
hdi::core::alerts::message("This version of CADtools cannot export its settings");
}
}
OKsoFar = CTpi->cleanMessageData(*SPdata);
HDI_CORE_ILLUSTRATOR->free(ctVersData);
if (!OKsoFar || !gotIt) {
globals.ctSettings.enabled = false;
if (this->CTdlg.visible()) this->CTdlg.hide();
return false;
}
this->CTlabel.setText("Getting CADtools settings...");
hdi::ct::PublicMsgGetCurrentScaleData* ctScaleData = (hdi::ct::PublicMsgGetCurrentScaleData*) HDI_CORE_ILLUSTRATOR->alloc(sizeof(hdi::ct::PublicMsgGetCurrentScaleData));
ctScaleData->d.self = HDI_CORE_PLUGIN->spPluginRef();
ctScaleData->layer = HDI_CORE_ILLUSTRATOR->currentDocument()->currentLayer()->aiLayerHandle();
SPdata = reinterpret_cast<SPMessageData*>(ctScaleData);
OKsoFar = CTpi->prepMessageData(*SPdata);
if (OKsoFar){
SPmsgErr = CTpi->message(hdi::ct::publicMsgCADtoolsCaller, hdi::ct::publicMsgGetCurrentScaleSelector, SPdata, SPresult);
if (!SPmsgErr & !SPresult && ctScaleData->success){
gotIt = true;
globals.ctSettings.enabled = true;
globals.ctSettings.scaleName = ctScaleData->name;
globals.ctSettings.scaler = ctScaleData->scaler;
globals.ctSettings.fromValue = ctScaleData->fromValue;
globals.ctSettings.fromUnits = ctScaleData->fromUnits;
globals.ctSettings.toValue = ctScaleData->toValue;
globals.ctSettings.toUnits = ctScaleData->toUnits;
globals.ctSettings.relationship = ctScaleData->relationship;
}
else {
gotIt = false;
globals.ctSettings.scaleName = "Unavailable";
globals.ctSettings.scaler = 1;
globals.ctSettings.fromValue = 1;
globals.ctSettings.fromUnits = "?";
globals.ctSettings.toValue = 1;
globals.ctSettings.toUnits = "?";
globals.ctSettings.relationship = "=";
}
}
OKsoFar = CTpi->cleanMessageData(*SPdata);
HDI_CORE_ILLUSTRATOR->free(ctScaleData);
if (!OKsoFar || !gotIt) {
globals.ctSettings.enabled = false;
if (this->CTdlg.visible()) this->CTdlg.hide();
return false;
}
this->CTlabel.setText("Applying CADtools settings...");
hdi::ct::PublicMsgGetCurrentAxoViewData* ctAxoData = (hdi::ct::PublicMsgGetCurrentAxoViewData*) HDI_CORE_ILLUSTRATOR->alloc(sizeof(hdi::ct::PublicMsgGetCurrentAxoViewData));
ctAxoData->d.self = HDI_CORE_PLUGIN->spPluginRef();
SPdata = reinterpret_cast<SPMessageData*>(ctAxoData);
OKsoFar = CTpi->prepMessageData(*SPdata);
if (OKsoFar){
SPmsgErr = CTpi->message(hdi::ct::publicMsgCADtoolsCaller, hdi::ct::publicMsgGetCurrentAxoViewSelector, SPdata, SPresult);
if (!SPmsgErr && !SPresult && ctAxoData->success){
gotIt = true;
globals.ctSettings.enabled = true;
globals.ctSettings.axoName = ctAxoData->name;
globals.ctSettings.rotation = ctAxoData->rotation;
globals.ctSettings.tilt = ctAxoData->tilt;
globals.ctSettings.angleA = ctAxoData->angleA;
globals.ctSettings.angleB = ctAxoData->angleB;
globals.ctSettings.foreshorten = ctAxoData->foreshorten;
if (ctVers >= 110106) { // version 110106 (11.1.6) needed for scalers for axes
globals.ctSettings.zScaler = ctAxoData->fsA; // right
globals.ctSettings.xScaler = ctAxoData->fsB; // left
globals.ctSettings.yScaler = ctAxoData->fsC; // vertical
}
else {
globals.ctSettings.xScaler = globals.ctSettings.yScaler = globals.ctSettings.zScaler = 0.8165;
}
}
else { // enter isometric values as default
gotIt = false;
globals.ctSettings.axoName = "Unavailable";
globals.ctSettings.rotation = 45;
globals.ctSettings.tilt = 35.2644;
globals.ctSettings.angleA = 30;
globals.ctSettings.angleB = 30;
globals.ctSettings.foreshorten = 0.8165;
globals.ctSettings.xScaler = globals.ctSettings.yScaler = globals.ctSettings.zScaler = 0.8165;
}
}
OKsoFar = CTpi->cleanMessageData(*SPdata);
HDI_CORE_ILLUSTRATOR->free(ctAxoData);
if (!OKsoFar || !gotIt) {
globals.ctSettings.enabled = false;
if (this->CTdlg.visible()) this->CTdlg.hide();
return false;
}
this->applyCTdata();
if (this->CTdlg.visible()) this->CTdlg.hide();
return gotIt;
}
Also note that the strings for scale name, from units, and to units are only returned for custom user scales, not for the built in architectural or engineering scales. I hope it's helpful for others. Comments, of course, are welcome.