04-17-2019
05:51 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
04-17-2019
05:51 AM
Insert DXF or DWG into a planar sketch using COM/C++
In the API help there is some VB code to do this and I'm translating it into COM/C++.
I think I'm there but I've probably used a bad cast or a wrong pointer type because the very last part crashes
// THIS CALL CRASHES, I SUSPECT THE LAST PARAMETER hRes = pDwgTranslator->Open(pDataMedium, pTranslationContext, pOptions, (IDispatch**)&pPartDoc);
I also suspect this line of mine:
pTranslationContext->OpenIntoExisting = pNewSketch ;
I'm also not sure about...
CComPtr<ApplicationAddIn> pAppAddIn;
hRes = pAppAddIns->get_ItemById(CComBSTR("{C24E3AC2-122E-11D5-8E91-0010B541CD80}"), &pAppAddIn);
if (FAILED(hRes)) {
gLogger.Printf(ekErrMsg, L"get_ItemById failed");
return;
}
CComPtr<TranslatorAddIn> pDwgTranslator;
pDwgTranslator = pAppAddIn; // Is this correct?
The full fragment is here...
hRes = pInvApp->get_ApplicationAddIns(&pAppAddIns);
if (FAILED(hRes)) {
gLogger.Printf(ekErrMsg, L"get_ApplicationAddIns failed");
return;
}
CComPtr<ApplicationAddIn> pAppAddIn;
hRes = pAppAddIns->get_ItemById(CComBSTR("{C24E3AC2-122E-11D5-8E91-0010B541CD80}"), &pAppAddIn);
if (FAILED(hRes)) {
gLogger.Printf(ekErrMsg, L"get_ItemById failed");
return;
}
CComPtr<TranslatorAddIn> pDwgTranslator;
pDwgTranslator = pAppAddIn;
if (pDwgTranslator == nullptr) {
gLogger.Printf(ekErrMsg, L"pDwgTranslator = pAppAddIn did not work");
return;
}
CComPtr<TransientObjects> pTransientObjects = GetTransientObjectsPtr();
CComPtr<DataMedium> pDataMedium;
hRes = pTransientObjects->CreateDataMedium(&pDataMedium);
if (FAILED(hRes)) {
gLogger.Printf(ekErrMsg, L"CreateDataMedium failed");
return;
}
pDataMedium->put_FileName(CComBSTR("C:\\Users\\Owen\\TEST.dxf"));
CComPtr<TranslationContext> pTranslationContext;
hRes = pTransientObjects->CreateTranslationContext(&pTranslationContext);
if (FAILED(hRes)) {
gLogger.Printf(ekErrMsg, L"CreateTranslationContext failed");
return;
}
hRes = pNewSketch->Edit();
if (FAILED(hRes)) {
gLogger.Printf(ekErrMsg, L"Edit failed");
return;
}
// !!! I SUSPECT THIS !!!
pTranslationContext->OpenIntoExisting = pNewSketch ;
CComPtr<NameValueMap> pOptions;
hRes = pTransientObjects->CreateNameValueMap(&pOptions);
if (FAILED(hRes)) {
gLogger.Printf(ekErrMsg, L"Edit failed");
return;
}
// Specify the layers to import
pOptions->Add(BSTR("SelectedLayers"), CComVariant("0"));
pOptions->Add(BSTR("InvertLayersSelection"), CComVariant (VARIANT_TRUE));
// Specify the units
pOptions->Add(BSTR("FileUnits"), CComVariant("Centimeters"));
// ' Set to constraint the end points.
pOptions->Add(BSTR("ConstrainEndPoints"), CComVariant (VARIANT_TRUE));
// !!!THIS CALL CRASHES I SUSPECT THE LAST PARAMETER!!!
hRes = pDwgTranslator->Open(pDataMedium, pTranslationContext, pOptions, (IDispatch**)&pPartDoc);
if (FAILED(hRes)) {
gLogger.Printf(ekErrMsg, L"Open DataMedium failed");
return;
}
pNewSketch->ExitEdit();
04-19-2019
12:19 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
04-19-2019
12:19 AM
Can you share your C++ project to reproduce the problem? And let me which Inventor you are using. Don't share any confidential data here.
If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.

Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.
04-23-2019
12:19 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
04-23-2019
12:19 AM
Thanks for the reply, I'll knock together something simple over the next day or so...
04-23-2019
11:37 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
04-23-2019
11:37 PM
Here is a version with no error checking and fewer of own functions. A comment towards the end of the code shows where it crashes...
// Create a part
CComPtr<PartDocument> pPartDoc;
bool bOk = CreateNewPart(GetInvAppPtr(),pPartDoc,L"Test");
if (!bOk) {
return;
}
CComPtr<PartComponentDefinition> pPartDef;
pPartDoc->get_ComponentDefinition(&pPartDef);
CComPtr<PlanarSketch> pNewSketch;
CComPtr<WorkPlane> pWorkPlane;
// Get the work plane from the list of work planes in the part...
pPartDef->WorkPlanes->get_Item(CComVariant(1), &pWorkPlane);
// Get the list of sketches
CComPtr<PlanarSketches> pSketchList ;
pPartDef->get_Sketches (&pSketchList);
pSketchList->Add (_variant_t((IDispatch *)pWorkPlane),VARIANT_FALSE,&pNewSketch) ;
const CString kcsSketchName (L"SketchTest");
pNewSketch->put_Name (CComBSTR (kcsSketchName)) ;
// Add a dxf file
CComPtr<Application> pInvApp = GetInvAppPtr();
CComPtr<ApplicationAddIns> pAppAddIns;
pInvApp->get_ApplicationAddIns(&pAppAddIns);
CComPtr<ApplicationAddIn> pAppAddIn;
pAppAddIns->get_ItemById(CComBSTR("{C24E3AC2-122E-11D5-8E91-0010B541CD80}"), &pAppAddIn);
CComPtr<TranslatorAddIn> pDwgTranslator;
pDwgTranslator = pAppAddIn;
CComPtr<TransientObjects> pTransientObjects = GetTransientObjectsPtr();
CComPtr<DataMedium> pDataMedium;
pTransientObjects->CreateDataMedium(&pDataMedium);
pDataMedium->put_FileName(CComBSTR("C:\\Users\\Owen\\Documents\\Temp\\Drawing1.dxf"));
CComPtr<TranslationContext> pTranslationContext;
pTransientObjects->CreateTranslationContext(&pTranslationContext);
pNewSketch->Edit();
// !!! I SUSPECT THIS !!!
pTranslationContext->OpenIntoExisting = pNewSketch ;
CComPtr<NameValueMap> pOptions;
pTransientObjects->CreateNameValueMap(&pOptions);
// Specify the layers to import
pOptions->Add(BSTR("SelectedLayers"), CComVariant("0"));
pOptions->Add(BSTR("InvertLayersSelection"), CComVariant (VARIANT_TRUE));
// Specify the units
pOptions->Add(BSTR("FileUnits"), CComVariant("Centimeters"));
// ' Set to constraint the end points.
pOptions->Add(BSTR("ConstrainEndPoints"), CComVariant (VARIANT_TRUE));
// !!!THIS CALL CRASHES I SUSPECT THE LAST PARAMETER!!!
pDwgTranslator->Open(pDataMedium, pTranslationContext, pOptions, (IDispatch**)&pPartDoc);
pNewSketch->ExitEdit();