Menu

Problem adding shapes to a scaled diagram

iwbnwif
2014-02-06
2014-03-31
  • iwbnwif

    iwbnwif - 2014-02-06

    I have been having problems adding shapes programatically after a diagram has been scaled. Basically they end up in the wrong position.

    This is my code:

        m_pCanvas->AddStyle(wxSFShapeCanvas::sfsGRID_SHOW);
        m_pCanvas->AddStyle (wxSFShapeCanvas::sfsGRID_USE);
        m_pCanvas->RemoveStyle (wxSFShapeCanvas::sfsMULTI_SELECTION);
        m_pCanvas->AddStyle (wxSFShapeCanvas::sfsGRADIENT_BACKGROUND);
        m_pCanvas->AddStyle(wxSFShapeCanvas::sfsPROCESS_MOUSEWHEEL);
    
        m_pCanvas->SetGradientFrom (*wxWHITE);
        m_pCanvas->SetGradientTo (*wxWHITE);
        m_pCanvas->EnableGC(true);
    
        wxSFRectShape* shape1 = (wxSFRectShape*)m_Manager.AddShape(CLASSINFO(wxSFRectShape), wxPoint(100, 100), false);
        wxSFRectShape* shape2 = (wxSFRectShape*)m_Manager.AddShape(CLASSINFO(wxSFRectShape), wxPoint(300, 100), false);
    
        m_pCanvas->SetScale(0.5);
    
        m_Manager.UpdateAll();
        m_pCanvas->Refresh();
    
        wxSFRectShape* shape3 = (wxSFRectShape*)m_Manager.AddShape(CLASSINFO(wxSFRectShape), wxPoint(300, 300), false);
    

    The last shape added ends up in the wrong position. I can work around the problem by adding a MoveTo, i.e:

        shape3->MoveTo(300,300);
    

    I think the problem is in DiagramManager.cpp line 134. If I change it to:

    wxPoint newPos = m_pShapeCanvas->FitPositionToGrid(pos);
    

    Everything is fine.

     
  • Michal Bližňák

    The AddShape() functions use "device positions" so if you want to specify the position of newly added shape in "logical" coordinates, you must re-calculate it in accordance to the current canvas settings. You can do it via wxSFShapeCanvas::LP2DP() function. Please, try the following command:

    wxSFRectShape shape3 = (wxSFRectShape)m_Manager.AddShape(CLASSINFO(wxSFRectShape), m_pCanvas->LP2DP(wxPoint(300, 300)), false);

     
  • iwbnwif

    iwbnwif - 2014-03-31

    Ah thank you for the explanation, your suggestion works fine (with a little bit of rounding errors).

    Thanks once again!

     

    Last edit: iwbnwif 2014-03-31

Log in to post a comment.