«

C++ 工程开发笔记

向田欢 发布于 阅读:48


一、容器(数组 / 集合)

std::vector<action_t*> delAct;// 动态数组,允许重复,连续存储,按插入顺序
std::set<action_t*> delAct;// 自动去重,元素唯一,默认按指针地址升序排序
std::unordered_set<action_t*> delAct;// 无序去重,查询更快,不需要有序时优先使用
std::map<double, double, std::less<double>> mapUp; // 正序
std::map<double, double, std::greater<double>> mapDown; // 倒序

二、内存安全删除宏

SAFE_DELETE (ptr); // 删除单个对象
SAFE_DELETE_ARR (ptr); // 删除数组
SAFE_DELETE_EX (ptr); // 扩展删除
SAFE_DELETE_LIST_ACTION; // 删除 action 链表

三、类型转换与判断

// 强转子类型
子类型 * psur = (子类型 *) entry;子类型 * psur = static_cast < 子类型 *>(entry);
// 动态转换(安全)
CFileAperture* pap = dynamic_cast<CFileAperture*>(ap);
// 判断是否为文件光圈
if (typeid (*pac->get_aperture ()) == typeid (CFileAperture))if (pac->get_aperture ()->Type () == EAperture::eFile_Aper)

四、STEP 操作

// 获取当前 STEPCFileTrans* pTrans = CCAMUI::GetCAMUI ()->GetFileTran ();
// 根据名称获取 STEPCFileTrans* pDestStep = CScenceManager::GetManager ()->GetStep (name);
// 新建 STEPCScenceManager::GetManager ()->CreateStep (m_StepImp);
// 删除 STEPCScenceManager::GetManager ()->RemoveStep (pNetStep);
// 切换 STEPCCAMUI::GetCAMUI ()->SwitchStep (m_StepImp);
// 同步到列表CScenceManager::GetManager ()->GetMatrixHelper ()->AppendStep (m_StepImp);

五、层(Layer)操作

// 创建或清空层
CFileCell* pLayer = nullptr;CScenceManager::GetManager ()->CreateOrClearLayer (m_pTrans, pLayer, "层名");
// 添加元素、刷新
pLayer->add_tail_1 (action);pLayer->Segmentation ();pLayer->SetChanged (true);
// 根据层名查找
CFileCell* pCell = m_pTrans->GetFileCellByLayerName ("层名");
// 获取当前激活层
CFileCell* pCell = CCAMUI::GetCAMUI ()->GetActiveFile ();
// 删除空线路层
for (auto layer : pTrans->get_layers ()){
if (IsSignalLayer (layer) && list_count (&layer->actionsHead)==0){
pTrans->remove_layer (layer);
}
// 遍历层内元素
for (list_head* pos = pLayer->actionsHead.next; pos != &pLayer->actionsHead; pos=pos->next){action_t* pAct = list_entry (pos, action_t, node);}
// 安全遍历list_head *pos,n;
list_for_each_safe(pos,n,&pLayer->actionsHead){
action_t pEle = list_entry(pos,action_t,node)->clone();}

六、光圈与 Action 操作

// 创建光圈
aperture_t* pNewApe = DefineAperturecEx (str);
// 获取光圈
aperture_t* pApe = pCell->GetAperture ("r10",true,MM_TO_COORD);
// 光圈缩放
IAperture* newApe = resize_aperture (pap,0.1*MIL_TO_COORD,pCell);
// 打散文件光圈(SYB)
(new CFileODB ())->BreakPad (pflash,eleNewArr,true,pCell);
// 读取文件光圈
vector<action_t*> eleNewArr;CFileAperture* pap = (CFileAperture*) pac->get_aperture ();pap->GetApertureDetailAper (eleNewArr,0);
// Action 距离与交点
double action_distance (a0,a1,out0,out1);
double action_distance_noWidth (a0,a1);
void action_intersect (a0,a1,inters);
void seg_action_intersect_sur (a0,a1,inters);
// 包含 / 包围 / 连接判断bool action_isInclude (a0,a1);bool action_iscovered (a0,a1);bool is_action_connection (a0,a1);
// 范围搜索RectCell rt;vector<action_t*> actionSet;pCell->cross_range_searchv (rt,actionSet,action_t_filter ());

七、铜皮(Surface)操作

// 优化轮廓action_surface* sur = (action_surface*) ac;sur->OptimizeOutcounter ();sur->OptimizeIncounter ();
// 铜皮合并
UnionHelperPtr pUionnp = GetUnionHelper ();
pUionnp->AddItems (pSetSurf.begin (),pSetSurf.end ());
pUionnp->UnionOper ();
// 铜皮掏除(差集)
pUionnp->AddItem (pOutSurf);
pUionnp->AddItems2 (pPcsSurf.begin (),pPcsSurf.end ());
pUionnp->DiffSectOper ();
// 铜皮偏移(放大 / 缩小)
COffesetSurface os;vector<action_surface*> outSurfaces;
os.offeset_surface (psur,10MIL_TO_COORD,outSurfaces); // 放大
os.offeset_surface (psur,-10MIL_TO_COORD,outSurfaces); // 缩小
// 去尖角
os.remove_sharp_corners (psur,size,outSurfaces);
// 铜皮转轮廓线
for (auto pos=psurface->get_out_action_head ()->next;
    pos!=psurface->get_out_action_head ();
    pos=pos->next){action_t* ac = list_entry (pos,action_t,node)->clone ();
    ac->set_aperture (ap);pLayer->add_tail_1 (ac);
}

八、几何计算

// 两点距离hypot (dx,dy);vertex2d (e-s).length ();
// 质心Point computeCentroid (const vector<Point>& points);
// 斜率double calculateSlope1 (p1,p2);
// 判断平行bool areLineSegmentsParallel1 (p1,p2,p3,p4,tolerance);
// 点在线段上bool onSegment (p,q,r,epsilon);
// 坐标相等is_point_equal (p1,p2);
// 凸包vector<vertex2d> GetConvexHull(const vector<vertex2d>& polygon);
// 叉积double crossProduct (a,b,c);

九、单位转换

MM_TO_COORD // 毫米→内部坐标COORD_TO_MIL // 内部坐标→密耳MIL_TO_COORD // 密耳→内部坐标

十、字符串操作

str.MakeUpper (); // 转大写str.MakeLower (); // 转小写
// CString 格式化CString lyName;lyName.Format (_T ("r% s"),CStringA (size.c_str ()));
// 不区分大小写比较if (pCell->GetLayerName ().CompareNoCase (name)==0)

十一、调试与日志

OutputDebugString ("信息"); // 调试输出kcout << "内容"; // 日志打印AfxMessageBox ("提示"); // 弹窗

十二、Lambda 函数

auto concat = 
{return a+b;};auto add = 
{return x+y;};auto func = {/代码/};

十三、JSON 解析

Json::Reader reader;Json::Value root;reader.parse(arg,root);Json::Value dicts = root["data"]["dicts"];

十四、网络抽取

gkolinesCell->extract_net ();for (auto& net : gkolinesCell->GetShapeBlocks ()){// 按网络创建层}

十五、其他常用

// 智能指针
unique_ptr<IpcbCommandExecution> m_ipcbcom1;
// 保存工程
CSaveMgr::GetInstance ()->Save ();
// 获取线路层 / 钻孔层
vector<CFileCell*> sigArr = ACTIVESTEP->GetAllSignalLayers ();
GetThroughFileCellDrill (pCell,vecDrills);
// 层类型枚举转换
const char* KjGetLayerTypeText (PcbLayerType lyType);
PcbLayerType KjGetLayerTypeEnum (CString const& str)