AnsweredAssumed Answered

Cpp Changing name of configuration

Question asked by Sergey Kolesnik on Feb 13, 2018
Latest reply on Feb 13, 2018 by Sergey Kolesnik

Hello. I am trying to change name of current configuration without any success.

API says there are methods get_Name and set_Name for IConfiguration, also EditConfiguration3 for IModelDoc2.

So, there's another one method instead "set" - put_Name.

The problem is that IConfiguration object succesfuly retrieves current Name with get_Name, but it does not change it with put_Name. Neither does EditConfiguration3, which throws access violation.

Here's my code, I hope somebody would help with advice.

 

#include "stdafx.h"

#include "atlbase.h"

#include <iostream>

#include <string>

 

#import "sldworks.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids

#import "swconst.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids

 

int _tmain(int argc, _TCHAR* argv[])

{

CoInitialize(NULL);

{

 

CComPtr<ISldWorks> swApp;

CComPtr<IModelDoc2> swModel;

 

if (swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER) != S_OK)

{

return(0);

}

else std::cout << "Initialization OK." << std::endl;

swApp->put_UserControl(VARIANT_TRUE);

swApp->put_Visible(VARIANT_TRUE);

 

swApp->get_IActiveDoc2(&swModel);

 

if (!swModel) return(0);

 

CComBSTR strDocTitle;

CComBSTR strDocPath;

 

long nbrConfigs;

long nDocType;

 

//Configuration properties

CComPtr<IConfigurationManager> pCfgMgr;

CComPtr<IConfiguration> pConf;

CComBSTR strConfName;

 

swModel->GetType(&nDocType);

swModel->GetTitle(&strDocTitle);

swModel->GetPathName(&strDocPath);

 

swModel->GetConfigurationCount(&nbrConfigs);

swModel->get_ConfigurationManager(&pCfgMgr);

pCfgMgr->get_ActiveConfiguration(&pConf);

pConf->get_Name(&strConfName);

 

std::string DocType;

 

if (nDocType == swDocASSEMBLY) DocType = "Assembly";

else if (nDocType == swDocPART) DocType = "Part";

else if (nDocType == swDocDRAWING) DocType = "Drawing";

else DocType = "Unknown";

 

CW2A DocTitle(strDocTitle);

CW2A DocPath(strDocPath);

CW2A ConfName(strConfName);

 

std::cout << "Current document is " << "Solidworks " << DocType << "document: " <<

DocTitle << std::endl;

std::cout << "File path:" << DocPath << std::endl << std::endl;

std::cout << "Number of configurations: " << nbrConfigs << '.' << std::endl;

std::cout << "Current configuration name is: " << ConfName << '.' << std::endl;

 

HRESULT vbool = S_OK;

 

//swModel->EditConfiguration3(strConfName, BSTR("New Name"), BSTR(""), BSTR(""), long(64), &vbool);

vbool = pConf->put_Name(BSTR("New Name"));

std::cout << vbool << std::endl;

 

}

 

 

CoUninitialize();

    return 0;

 

Sorry for not formatting code properly, there are some issues with showing it properly that way.

Outcomes