Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
oransen
in reply to: oransen

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();