#include "stdafx.h" #include "SigmaView.h" #include "SectionDoc.h" #include "ItemProportion.h" #include "ActionListItem.h" static CItemProportion * getItemProportion(CSigmaView * pView) { CItem * pItem = pView->GetItem(); if (pItem == NULL) return NULL; CItemProportion * proportionItem = dynamic_cast(pItem); return proportionItem; } extern "C" __declspec(dllexport) int CreateProportion(CSigmaView * pView) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; pItem->InsertProportion(); return 1; } extern "C" __declspec(dllexport) int Proportion_ChangeScale(CSigmaView * pView, double length) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeScale(length); } extern "C" __declspec(dllexport) int Proportion_ChangeScaleHeight(CSigmaView * pView, double height) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeScaleHeight(height); } extern "C" __declspec(dllexport) int Proportion_ChangeNumber(CSigmaView * pView, int number) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeNumber(number); } extern "C" __declspec(dllexport) int Proportion_ChangeShowMode(CSigmaView * pView, int mode) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeShowMode(mode); } extern "C" __declspec(dllexport) int Proportion_ChangeUnit(CSigmaView * pView, int unit) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeUnit(unit); } extern "C" __declspec(dllexport) int Proportion_ChangeColor(CSigmaView * pView, int r, int g, int b) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeColor(r, g, b); } extern "C" __declspec(dllexport) int Proportion_ChangeTextScale(CSigmaView * pView, double scale) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeTextScale(scale); } extern "C" __declspec(dllexport) int Proportion_ChangeAlign(CSigmaView * pView, int align) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeAlign(align); } extern "C" __declspec(dllexport) int Proportion_ChangeTextHeight(CSigmaView * pView, int height) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeTextHeight(height); } extern "C" __declspec(dllexport) int Proportion_ChangeTextWidth(CSigmaView * pView, int width) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeTextWidth(width); } extern "C" __declspec(dllexport) int Proportion_ChangeTextHeightAlone(CSigmaView * pView, int alone) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeTextHeightAlone(alone); } extern "C" __declspec(dllexport) int Proportion_ChangeCoordinate(CSigmaView * pView, double x, double y) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->ChangeCoordinate(x, y); } extern "C" __declspec(dllexport) int Proportion_Create(CSigmaView * pView) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->Add(); } extern "C" __declspec(dllexport) int Proportion_GetScale(CSigmaView * pView) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) return -1; return pItem->GetScale(); } extern "C" __declspec(dllexport) void Proportion_SetParameter(CSigmaView * pView, struct ProportionData * data) { CItemProportion * pItem = getItemProportion(pView); if (pItem == nullptr) { return; } pItem->SetParameter(data); } extern "C" __declspec(dllexport) int Proportion_GetData(CSigmaView * pView, struct ProportionData * data) { CItemProportion * pItem = getItemProportion(pView); if (pItem == NULL) { //data->ScaleFactor = pView->m_pDoc->GetDraw()->m_dScaleFactor; CUnit cu; double h = cu.Millimetres(pView->m_pDoc->GetDraw()->m_unit.cx); //根据1个实际单位的毫米长度 //1毫米代表的实际长度 double m_dScaleLength = pView->m_pDoc->GetDraw()->m_dScaleFactor / h; //转换为1厘米代表的实际长度 m_dScaleLength *= 10; m_dScaleLength = 2000; data->ScaleLength = m_dScaleLength; data->ColorR = 0; data->ColorG = 0; data->ColorB = 0; data->Unit = 2; data->TextSpace = 1; data->TextAlign = 1; data->TextScale = 1; return -1; } pItem->GetData(data); return 1; } /** * \brief 在图件中创建并完整初始化一个比例尺对象。 * 该函数是一个原子操作,涵盖了:计算对齐位置、自动创建图层、构造对象以及注册撤销记录(Undo)。 * 比例尺将水平居中放置在指定的 X 范围内,并根据 yMax 和 offset 确定垂直位置。 * \param pView 指向当前 Sigma 视图对象的指针,不可为 nullptr。 * \param layerName 目标图层的名称。若图层不存在,系统将自动创建该图层。 * \param xMin 布局参考矩形的左侧 X 坐标。 * \param yMin 布局参考矩形的底部 Y 坐标(用于几何参考)。 * \param xMax 布局参考矩形的右侧 X 坐标。 * \param yMax 布局参考矩形的顶部 Y 坐标(作为偏移基准)。 * \param scaleValue 比例尺分段代表的数值(单位:实际长度,内部按 /100.0 计算单位长度)。 * \param count 比例尺的总分段数(必须 >= 1)。 * \param offset 比例尺在 yMax 基础上的垂直偏移距离。 * \return true 创建成功并已添加到图件中。 * \return false 环境异常(如 pView 为空)或内部构造失败。 */ extern "C" __declspec(dllexport) bool CreateProportionFull( CSigmaView* pView, LPCSTR layerName, double xMin, double yMin, double xMax, double yMax, int scaleValue, int count, int offset) { if (pView == nullptr || pView->m_pDoc == nullptr || pView->m_pDoc->m_pXy == nullptr) { TRACE("%s: 非法 view 或者 document 指针\n", __FUNCTION__); return false; } auto pAction = std::make_unique(pView->m_pDoc, IDS_STRING_ACTION_DELETE); CXy* pXy = pView->m_pDoc->m_pXy; CLayer* pLayer = pXy->FindLayer(layerName); if (pLayer == nullptr) { const CString currentLayer = pXy->GetCurrentLayer()->GetPathName(); pLayer = pXy->FindAddLayer(layerName); pAction->AddLayerAddItem(pLayer, currentLayer); } auto pProportion = std::make_unique(); double unitLen = scaleValue / 100.0; double totalLen = unitLen * count; // 布局算法:水平居中于 [xMin, xMax],垂直偏移 yMax pProportion->x0 = xMin + (std::fabs(xMax - xMin) - totalLen) / 2.0; pProportion->y0 = yMax + offset; pProportion->m_dScaleLength = scaleValue / 100.0; pProportion->num = count; auto pOne = std::make_unique(); pOne->SetValueSafe(pProportion.release()); pOne->SetLayer(pLayer); POSITION pos = pXy->AddTailOne(pOne.release()); CPositionList items; items.AddTail(pos); pAction->AddAddItem(items); pView->m_pDoc->SetActionItem(pAction.release()); }