#include "stdafx.h" //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// // // BEGIN SampleTranslate command // //////////////////////////////////////////////////////////////////// // CRhinoGetTranslationPoint declaration class CRhinoGetTranslationPoint : public CRhinoGetPoint { public: CRhinoGetTranslationPoint(); ~CRhinoGetTranslationPoint() {} // CRhinoGetPoint overrides void SetBasePoint(ON_3dPoint base_point, BOOL bShowDistanceInStatusBar = false); void OnMouseMove(CRhinoViewport& vp, UINT flags, const ON_3dPoint& point, const ON_2iPoint* view_wnd_point); void DynamicDraw(CRhinoDisplayPipeline& dp, const ON_3dPoint& pt); // Additional helpers void AddObject(const CRhinoObject* object); void CalculateTranslation(const ON_3dPoint& pt, ON_Xform& xform); private: ON_3dPoint m_start_point; // starting point of translation ON_Xform m_xform; // transformation matrix ON_SimpleArray m_objects; //objects to transform }; //////////////////////////////////////////////////////////////////// // CRhinoGetTranslationPoint definition CRhinoGetTranslationPoint::CRhinoGetTranslationPoint() { m_xform = ON_Xform::IdentityTransformation; } void CRhinoGetTranslationPoint::AddObject(const CRhinoObject* object) { m_objects.Append(object); } void CRhinoGetTranslationPoint::SetBasePoint( ON_3dPoint base_point, BOOL bShowDistanceInStatusBar ) { m_start_point = base_point; CRhinoGetPoint::SetBasePoint(base_point, bShowDistanceInStatusBar); } void CRhinoGetTranslationPoint::CalculateTranslation( const ON_3dPoint& pt, ON_Xform& xform ) { ON_3dVector v = pt - m_start_point; if (v.IsTiny()) xform = ON_Xform::IdentityTransformation; else xform = ON_Xform::TranslationTransformation(v); } void CRhinoGetTranslationPoint::OnMouseMove(CRhinoViewport& vp, UINT flags, const ON_3dPoint& pt, const ON_2iPoint* view_wnd_point) { // Everytime the mouse moves, calculate the translation CalculateTranslation(pt, m_xform); CRhinoGetPoint::OnMouseMove(vp, flags, pt, view_wnd_point); } void CRhinoGetTranslationPoint::DynamicDraw(CRhinoDisplayPipeline& dp, const ON_3dPoint& pt) { // Time to draw our objects dynamically int i, count = m_objects.Count(); if (m_xform.IsIdentity() == false && count > 0) { ON_Color saved_color = dp.ObjectColor(); ON_Xform saved_model_xform; // Save the current model transformation, we will // need to restore it later dp.GetRhinoVP()->GetModelXform(saved_model_xform); // Set the model transformation to ours dp.SetModelTransform(m_xform); // Draw all of the objects in our array for (i = 0; i < m_objects.Count(); i++) { const CRhinoObject* object = m_objects[i]; if (object == 0) continue; dp.SetObjectColor(object->ObjectDrawColor(TRUE)); object->Draw(dp); if (dp.InterruptDrawing()) break; } // Reset modified viewport members dp.GetRhinoVP()->SetModelXform(saved_model_xform); dp.SetObjectColor(saved_color); } // Let the base class do its drawing too CRhinoGetPoint::DynamicDraw(dp, pt); } //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// class CCommandSampleTranslate : public CRhinoCommand { public: CCommandSampleTranslate() = default; ~CCommandSampleTranslate() = default; UUID CommandUUID() override { // {FA74E332-EC73-4137-B2DF-DCEFA39A6C83} static const GUID SampleTranslateCommand_UUID = { 0xfa74e332, 0xec73, 0x4137, { 0xb2, 0xdf, 0xdc, 0xef, 0xa3, 0x9a, 0x6c, 0x83 } }; return SampleTranslateCommand_UUID; } const wchar_t* EnglishCommandName() override { return L"SampleTranslate"; } CRhinoCommand::result RunCommand(const CRhinoCommandContext& context) override; }; // The one and only CCommandSampleTranslate object static class CCommandSampleTranslate theSampleTranslateCommand; CRhinoCommand::result CCommandSampleTranslate::RunCommand(const CRhinoCommandContext& context) { CRhinoGetObject go; go.SetCommandPrompt(L"Select objects to move"); go.GetObjects(1, 0); if (go.CommandResult() != CRhinoCommand::success) return go.CommandResult(); CRhinoGetPoint gp; gp.SetCommandPrompt(L"Point to move from"); gp.GetPoint(); if (gp.CommandResult() != CRhinoCommand::success) return gp.CommandResult(); CRhinoGetTranslationPoint gt; gt.SetCommandPrompt(L"Point to move to"); gt.SetBasePoint(gp.Point()); gt.DrawLineFromPoint(gp.Point(), TRUE); gt.AddObject(go.Object(0).Object()); gt.GetPoint(); if (gt.CommandResult() != CRhinoCommand::success) return gp.CommandResult(); CRhinoView* view = gt.View(); if (0 == view) return CRhinoCommand::failure; ON_Xform xform; gt.CalculateTranslation(gt.Point(), xform); for (int i = 0; i < go.ObjectCount(); i++) { CRhinoObjRef obj_ref = go.Object(i); context.m_doc.TransformObject(obj_ref, xform); } context.m_doc.Redraw(); return CRhinoCommand::success; } // // END SampleTranslate command // //////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////