#include "stdafx.h" //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// // // BEGIN cmdDES_Test command // #pragma region cmdDES_Test command class CCommandcmdDES_Test : public CRhinoCommand { public: CCommandcmdDES_Test() = default; UUID CommandUUID() override { // {BA624CD2-64ED-4DEE-A64A-D5DFC44817C4} static const GUID cmdDES_TestCommand_UUID = { 0xba624cd2, 0x64ed, 0x4dee,{ 0xa6, 0x4a, 0xd5, 0xdf, 0xc4, 0x48, 0x17, 0xc4 } }; return cmdDES_TestCommand_UUID; } const wchar_t* EnglishCommandName() override { return L"DES_TextureTest"; } CRhinoCommand::result RunCommand(const CRhinoCommandContext& context) override; }; // The one and only CCommandcmdDES_Test object static class CCommandcmdDES_Test thecmdDES_TestCommand; bool CreateDefaultTextureMapping(const CRhinoObject& obj, const int channelId, const UUID& pluginUuid, UUID& uuidOut) { CRhinoDoc* pDoc = obj.Document(); if (NULL == pDoc) return false; ON_TextureMapping tm; tm.SetSurfaceParameterMapping(); const int index = pDoc->m_texture_mapping_table.AddTextureMapping(tm); if (index < 0) return false; const UUID uuidMapping = pDoc->m_texture_mapping_table[index].Id(); ON_3dmObjectAttributes attributes = obj.Attributes(); if (!attributes.m_rendering_attributes.AddMappingChannel(pluginUuid, channelId, uuidMapping)) { if (!attributes.m_rendering_attributes.DeleteMappingChannel(pluginUuid, channelId)) return false; if (!attributes.m_rendering_attributes.AddMappingChannel(pluginUuid, channelId, uuidMapping)) return false; } if (!pDoc->ModifyObjectAttributes(CRhinoObjRef(&obj), attributes, true)) return false; uuidOut = uuidMapping; return true; } const ON_MappingChannel* GetTextureMappingChannel(const CRhinoObject* pObject, const int channelId, const UUID & pluginUuid) { if (NULL == pObject) return NULL; const ON_MappingRef* pMapRef = pObject->Attributes().m_rendering_attributes.MappingRef(pluginUuid); if (NULL == pMapRef) return NULL; for (int i = 0; i < pMapRef->m_mapping_channels.Count(); i++) { if (pMapRef->m_mapping_channels[i].m_mapping_channel_id == channelId) return &(pMapRef->m_mapping_channels[i]); } return NULL; } bool SetCustomMeshMapping(const CRhinoObject& object, CRhinoDoc& doc, ON_Mesh* pCustomMappingMesh) { ON_UUID uuidRenderPlugIn = RhinoApp().GetDefaultRenderApp(); const ON_MappingChannel* pMappingChannel = GetTextureMappingChannel(&object, 1, uuidRenderPlugIn); if (nullptr == pMappingChannel) { UUID uuidMapping; CreateDefaultTextureMapping(object, 1, uuidRenderPlugIn, uuidMapping); pMappingChannel = GetTextureMappingChannel(&object, 1, uuidRenderPlugIn); } const int iTextureMapping = doc.m_texture_mapping_table.FindTextureMapping(pMappingChannel); if (iTextureMapping < 0 || iTextureMapping >= doc.m_texture_mapping_table.TextureMappingCount()) { delete pCustomMappingMesh; return false; } ON_TextureMapping textureMapping = doc.m_texture_mapping_table[iTextureMapping]; textureMapping.m_Pxyz = ON_Xform::IdentityTransformation; textureMapping.m_Nxyz = ON_Xform::IdentityTransformation; textureMapping.m_uvw = ON_Xform::IdentityTransformation; textureMapping.m_projection = ON_TextureMapping::PROJECTION::clspt_projection; textureMapping.m_type = ON_TextureMapping::TYPE::mesh_mapping_primitive; pCustomMappingMesh->Transform(pMappingChannel->m_object_xform.Inverse()); textureMapping.SetCustomMappingPrimitive(pCustomMappingMesh); if (!doc.m_texture_mapping_table.ModifyTextureMapping(textureMapping, iTextureMapping)) { delete pCustomMappingMesh; return false; } const ON_Xform * pObjectXform = (pMappingChannel == nullptr ? nullptr : &pMappingChannel->m_object_xform); object.SetTextureCoordinates(textureMapping, pObjectXform, false); return true; } CRhinoCommand::result CCommandcmdDES_Test::RunCommand(const CRhinoCommandContext& context) { //Set Rendered display mode CRhinoViewport& vp = ::RhinoApp().ActiveView()->ActiveViewport(); if (vp.View().m_display_mode_id != ON_StandardDisplayModeId::Rendered) { vp.SetDisplayMode(ON_StandardDisplayModeId::Rendered); ::RhinoApp().ActiveDoc()->Redraw(CRhinoView::regenerate_display_hint); } CRhinoGetObject get; get.SetGeometryFilter(CRhinoObject::surface_object); get.AcceptNothing(true); get.SetCommandPrompt(L"Select Surface to Texture Mapping"); CRhinoObjRef objRef; { for (;;) { context.m_doc.UnselectAll(); context.m_doc.Redraw(); CRhinoGet::result res = get.GetObjects(1, 1); if (res == CRhinoGet::object) { objRef = get.Object(0); break; } else if (res == CRhinoGet::cancel) return CRhinoCommand::cancel; } } const CRhinoObject* pObject = objRef.Object(); if (pObject) { CString m_BmpFile; CRhinoGetFileDialog GetFileDlg; GetFileDlg.DisplayFileDialog(CRhinoGetFileDialog::open_bitmap_dialog, ON_wString(m_BmpFile)); m_BmpFile = GetFileDlg.FileName(); //Texture mapping if (m_BmpFile != L"") { const CRhinoObjectAttributes& A = pObject->Attributes(); const ON_Material Material = pObject->ObjectMaterial(); CRhinoObjectAttributes A2(A); ON_Material M(Material); M.DeleteTexture(NULL, ON_Texture::TYPE::bitmap_texture); ON_Texture tex; tex.m_image_file_reference.SetFullPath(m_BmpFile, false); tex.m_bOn = true; tex.m_type = ON_Texture::TYPE::bitmap_texture; tex.m_mode = ON_Texture::MODE::decal_texture; M.AddTexture(tex); // No need to const cast object pointer. Modify attributes via document. context.Document()->ModifyObjectAttributes(objRef, A2); CRhinoMaterialTable& material_table = ::RhinoApp().ActiveDoc()->m_material_table; material_table.ModifyMaterial(M, A2.m_material_index, false); const ON_Brep* Brep = ON_Brep::Cast(pObject->Geometry()); ON_BrepFace* F = Brep->Face(0); // Don't modify render mesh itself. Modify duplicate of it instead. ON_Mesh* mappingMesh = F->Mesh(ON::render_mesh)->Duplicate(); double ScaleX = 5., ScaleY = 5.; ON_2fPointArray Texs = mappingMesh->m_T; for (int i = 0; im_T.Count(); i++) { ON_2fPoint* fpnt = Texs.At(i); double x = fpnt->x / ScaleX; double y = fpnt->y / ScaleY; fpnt->x = float(x); fpnt->y = float(y); } mappingMesh->m_T = Texs; // Set the modified mesh as mesh mapping primitive SetCustomMeshMapping(*pObject, *context.Document(), mappingMesh); } } ::RhinoApp().ActiveDoc()->Regen(); return CRhinoCommand::success; } #pragma endregion // // END cmdDES_Test command // //////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////