AnsweredAssumed Answered

How to transform components? C++

Question asked by Robert Schroeder on Nov 19, 2019
Latest reply on Dec 2, 2019 by Robert Schroeder

Hello,

I am using SW 2016 with C++ API. I want to add compontens (currently with swAssemblyDoc->IAddComponents3) to an assembly and apply new transformations.

 

I tried different approaches to apply a transform to the component:

  • IComponent2->put_Transform2, 
  • IComponent2->SetTransformAndSolve2, 
  • IComponent2->put_PresentationTransform

 

None of them works, and with the first approach (which I guess should be the correct way), I am facing the exact same problem as posted here in 2008:

https://translate.google.com/translate?hl=&sl=auto&tl=en&u=https%3A%2F%2Fww3.cad.de%2Fforen%2Fubb%2FForum2%2FHTML%2F0157… 

Unfortunately the thread has not been solved.

 

The problems:

  1. IComponent2->get_Transform2 returns the vector (1,0,0, 0,0,0 ,0,0,0, 0,0,0, 0, 0,0,0)
  2. None of the transform functions change anything
  3. There is sometimes, randomly some weird/wrong transformation applied, but not in a proper way. Yes, I used small values and checked transformations in single different directions.

 

I also tried swModelDocExt->Rebuild, swModel->EditRebuild3, swModel->GraphicsRedraw2 in case the transformed result is only displayed after those calls.

I know that the transform in swAssemblyDoc->IAddComponents3 is not applied if the original transformation of the added part is zero, which is the case.

 

Does anyone know a solution to this?

 

 

 

Here is the code for the first approach:

 

    this->swAssemblyDoc = this->swModel;

 

    // for every saved part
    for (size_t i = 0; i < this->parts_computation_path_saved.size(); i++)
    {

         // set up some test transform vector

        double dTransform[16] =
        {
            1., 0., 0.,  0., 1., 0.,  0.,  0.,  1.,

            double(i) * 2.,    // transl x
            double(i) * 0.,    // transl y
            double(i) * 0.,    // transl z

            1., // scale

            0., 0.,  0.
        };

      

         // set up BSTR for open files

        std::string path_open_act;
        path_open_act = this->parts_computation_path_saved[i];
        CComBSTR path_open_part;
        path_open_part = ConvertMBSToBSTR(path_open_act);

      

         // new IComponent2

        CComPtr<IComponent2> new_component;

 

        CComBSTR cosy_name(L"");

 

        // add component
        hres = this->swAssemblyDoc->IAddComponents3(1, &path_open_part, dTransform, 1, &cosy_name, &new_component); // <--------------- part is added to origin of assembly, dTransform is not taken into account, according to API documentation

 

        CComPtr<IMathUtility> util;

        swApp->IGetMathUtility(&util);


        CComPtr<IMathTransform> transform_act;
        CComPtr<IMathTransform> transform_final;

 

        hres = util->ICreateTransform(dTransform, &transform_final);

 

        new_component->get_Transform2(&transform_act); // <--------------- Here the output is (1,0,....,0) vector
        hres = util->ICreateTransform(dTransform, &transform_final);

        hres = new_component->put_Transform2(transform_final); // <--------------- Here nothing happens

}

Outcomes