打印vector<wstring>数据、
时间: 2023-12-11 19:02:17 浏览: 23
可以使用以下代码打印一个 `vector<wstring>` 类型的数据:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<wstring> vec = {L"你好", L"世界", L"!"};
for (wstring str : vec) {
wcout << str << endl;
}
return 0;
}
```
输出结果为:
```
你好
世界
!
```
需要注意的是,因为是 `wstring` 类型,所以需要使用 `wcout` 来输出。另外,使用循环遍历 `vector` 中的每个元素,然后使用 `wcout` 输出即可。
相关问题
objectarx二次开发自定义实体,实体数据是一个结构体tagGmyMapTable,结构体tagGmyMapTable中包含另外一个结构体tagGeometryWithID、一个类指针ElmGraphic*、一个bool变量,而结构体tagGeometryWithID保存的是一个unsigned long long int数据和一个vector<wstring>.类指针ElmGraphic*中有一个vector<shared_ptr<CBaseGeometry>> ptrgeos, CBaseGeometry是个基类,使用时会根据CBaseGeometry中的int型type的参数判断强制转换成不同子类进行绘制,子类中的数据也不相同,CBaseGeometry中包含了vector<AcGePoint3D>以及字符串、int等数据,请问如何保存读取这些数据到dwg
首先,需要定义一个继承自 AcDbEntity 的实体类 GmyMapTableEntity,然后在 GmyMapTableEntity 类中定义一个成员变量,类型为 tagGmyMapTable 结构体,用于保存实体数据。
在实现 GmyMapTableEntity 类的 dwg 文件读写方法时,可以将 tagGmyMapTable 结构体的成员变量依次读写到 dwg 文件中。
具体实现方法如下:
1. 在 GmyMapTableEntity.h 文件中定义 GmyMapTableEntity 类,并在类定义中添加 tagGmyMapTable 结构体成员变量。
```C++
class GmyMapTableEntity : public AcDbEntity
{
public:
GmyMapTableEntity();
virtual ~GmyMapTableEntity();
// dwg 文件读写方法
virtual Acad::ErrorStatus dwgInFields(AcDbDwgFiler* filer) override;
virtual Acad::ErrorStatus dwgOutFields(AcDbDwgFiler* filer) const override;
private:
tagGmyMapTable m_gmyMapTableData; // 实体数据
};
```
2. 在 GmyMapTableEntity.cpp 文件中实现 dwg 文件读写方法。具体实现过程如下:
- dwgInFields 方法
```C++
Acad::ErrorStatus GmyMapTableEntity::dwgInFields(AcDbDwgFiler* filer)
{
assertReadEnabled();
Acad::ErrorStatus es = AcDbEntity::dwgInFields(filer);
if (es != Acad::eOk) return es;
// 读取 tagGmyMapTable 结构体中的数据
es = filer->readItem(&m_gmyMapTableData.geometryData);
if (es != Acad::eOk) return es;
// 读取 ElmGraphic* 中的数据
if (filer->filerStatus() == Acad::eOk && m_gmyMapTableData.pElmGraphic != nullptr)
{
// 读取 vector<shared_ptr<CBaseGeometry>> 中的数据
Adesk::UInt32 count = 0;
filer->readUInt32(&count);
for (Adesk::UInt32 i = 0; i < count; i++)
{
// 读取 CBaseGeometry::type
int type = 0;
filer->readInt32(&type);
// 根据 CBaseGeometry::type 创建对应的子类对象
std::shared_ptr<CBaseGeometry> pGeometry;
if (type == CLine::Type)
{
pGeometry = std::make_shared<CLine>();
}
else if (type == CCircle::Type)
{
pGeometry = std::make_shared<CCircle>();
}
else if (type == CArc::Type)
{
pGeometry = std::make_shared<CArc>();
}
else
{
pGeometry = std::make_shared<CBaseGeometry>();
}
// 读取子类对象的数据
if (filer->filerStatus() == Acad::eOk)
{
pGeometry->dwgInFields(filer);
}
// 将子类对象添加到 vector<shared_ptr<CBaseGeometry>> 中
m_gmyMapTableData.pElmGraphic->ptrgeos.push_back(pGeometry);
}
// 读取 bool 变量
filer->readBool(&m_gmyMapTableData.bFlag);
}
return filer->filerStatus();
}
```
- dwgOutFields 方法
```C++
Acad::ErrorStatus GmyMapTableEntity::dwgOutFields(AcDbDwgFiler* filer) const
{
assertReadEnabled();
Acad::ErrorStatus es = AcDbEntity::dwgOutFields(filer);
if (es != Acad::eOk) return es;
// 写入 tagGmyMapTable 结构体中的数据
es = filer->writeItem(m_gmyMapTableData.geometryData);
if (es != Acad::eOk) return es;
// 写入 ElmGraphic* 中的数据
if (filer->filerStatus() == Acad::eOk && m_gmyMapTableData.pElmGraphic != nullptr)
{
// 写入 vector<shared_ptr<CBaseGeometry>> 中的数据
Adesk::UInt32 count = static_cast<Adesk::UInt32>(m_gmyMapTableData.pElmGraphic->ptrgeos.size());
filer->writeUInt32(count);
for (const auto& pGeometry : m_gmyMapTableData.pElmGraphic->ptrgeos)
{
// 写入 CBaseGeometry::type
int type = pGeometry->type();
filer->writeInt32(type);
// 写入子类对象的数据
if (filer->filerStatus() == Acad::eOk)
{
pGeometry->dwgOutFields(filer);
}
}
// 写入 bool 变量
filer->writeBool(m_gmyMapTableData.bFlag);
}
return filer->filerStatus();
}
```
3. 在主程序中,使用 AcDbBlockTableRecord::newEntity 方法创建 GmyMapTableEntity 实体对象,并将实体数据保存到 tagGmyMapTable 结构体成员变量中。
```C++
void createGmyMapTableEntity()
{
// 创建 GmyMapTableEntity 实体对象
GmyMapTableEntity* pGmyMapTableEntity = new GmyMapTableEntity;
// 设置实体数据
pGmyMapTableEntity->m_gmyMapTableData.geometryData.id = 1234567890;
pGmyMapTableEntity->m_gmyMapTableData.geometryData.name = L"MyMapTable";
pGmyMapTableEntity->m_gmyMapTableData.pElmGraphic = new ElmGraphic;
pGmyMapTableEntity->m_gmyMapTableData.bFlag = true;
// 设置 CLine 对象的数据
std::shared_ptr<CLine> pLine = std::make_shared<CLine>();
pLine->setStartPoint(AcGePoint3d(0, 0, 0));
pLine->setEndPoint(AcGePoint3d(10, 0, 0));
pGmyMapTableEntity->m_gmyMapTableData.pElmGraphic->ptrgeos.push_back(pLine);
// 设置 CCircle 对象的数据
std::shared_ptr<CCircle> pCircle = std::make_shared<CCircle>();
pCircle->setCenter(AcGePoint3d(5, 5, 0));
pCircle->setRadius(5);
pGmyMapTableEntity->m_gmyMapTableData.pElmGraphic->ptrgeos.push_back(pCircle);
// 添加实体对象到块表记录中
AcDbBlockTableRecord* pBlockTableRecord = getCurrentSpaceBlockTableRecord();
pBlockTableRecord->appendAcDbEntity(pGmyMapTableEntity);
pGmyMapTableEntity->close();
}
```
上述代码中的 getCurrentSpaceBlockTableRecord 方法用于获取当前空间的块表记录对象。
这样,就可以实现自定义实体的保存和读取了。
objectarx二次开发自定义实体,实体数据是一个结构体tagGmyMapTable,结构体tagGmyMapTable中包含另外一个结构体tagGeometryWithID geoID、一个类指针ElmGraphic* geoData、一个bool变量isBatchDrawBroadWise,而结构体tagGeometryWithID保存的是一个unsigned long long int elementID数据和一个vector<wstring> exDataVec,类指针geoData中有一个vector<shared_ptr<CBaseGeometry>> ptrgeos, CBaseGeometry是个基类,使用时会根据CBaseGeometry中的int型type的参数判断强制转换成不同子类进行绘制,子类中的数据也不相同,CBaseGeometry中包含了vector<AcGePoint3D> Pts以及字符串、int等数据,请问如何保存读取这些数据到dwg
在使用ObjectARX二次开发自定义实体时,需要实现AcDbEntity的相关接口函数,以便在dwg文件中保存和读取实体数据。
对于结构体tagGmyMapTable和tagGeometryWithID中的数据,可以使用AcDbXrecord类来进行保存和读取。AcDbXrecord是一种特殊的实体,其内部可以保存任何类型的数据,包括结构体、类指针等。
以下是保存tagGmyMapTable数据到dwg文件的示例代码:
```cpp
void saveMapTableToDwg(const AcDbObjectId& id, const tagGmyMapTable& mapTable)
{
// 打开实体对象
AcDbEntityPtr pEnt(id, AcDb::kForWrite);
if (pEnt.isNull())
return;
// 创建Xrecord对象
AcDbXrecordPtr pXrec = new AcDbXrecord;
pXrec->setXlateReferences(true);
// 将tagGmyMapTable数据保存到Xrecord中
resbuf* pBuf = acutBuildList(
RTSHORT, 0, // 数据版本号
RTSHORT, sizeof(tagGmyMapTable), // 数据长度
RTENDBR);
pBuf->rbnext = acutBuildList(
RTLONG, mapTable.geoID, // 保存tagGeometryWithID数据
RTLONG, mapTable.isBatchDrawBroadWise,
RTLONG, mapTable.geoData->ptrgeos.size(),
RTENDBR);
pBuf->rbnext->rbnext = acutBuildList(
RTLONG, mapTable.geoData->type,
RTLONG, mapTable.geoData->Pts.size(),
RTENDBR);
pBuf->rbnext->rbnext->rbnext = acutBuildList(
RTSTR, mapTable.geoData->name.c_str(),
RTSTR, mapTable.geoData->desc.c_str(),
RTLONG, mapTable.geoData->color,
RTENDBR);
pXrec->setFromRbChain(*pBuf);
acutRelRb(pBuf);
// 将Xrecord对象保存到实体中
pEnt->setXData(pXrec);
}
```
同样,可以使用AcDbXrecord类将tagGeometryWithID和CBaseGeometry的数据保存到dwg文件中。
读取数据时,需要先读取AcDbXrecord对象,然后解析其中的数据,重新构造出原来的结构体、类指针等。
以下是从dwg文件中读取tagGmyMapTable数据的示例代码:
```cpp
tagGmyMapTable getMapTableFromDwg(const AcDbObjectId& id)
{
tagGmyMapTable mapTable;
// 打开实体对象
AcDbEntityPtr pEnt(id, AcDb::kForRead);
if (pEnt.isNull())
return mapTable;
// 读取Xrecord对象
AcDbObjectIdArray xrecIds;
pEnt->getXData(xrecIds);
AcDbXrecordPtr pXrec;
if (xrecIds.length() > 0)
acdbOpenObject(pXrec, xrecIds[0], AcDb::kForRead);
if (pXrec.isNull())
return mapTable;
// 解析Xrecord中的数据
resbuf* pBuf = pXrec->rbChain();
if (pBuf == nullptr)
return mapTable;
// 读取tagGeometryWithID数据
if (pBuf->restype == RTSHORT && pBuf->resval.rint == 0)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTSHORT || pBuf->resval.rint != sizeof(tagGeometryWithID))
return mapTable;
tagGeometryWithID geoID;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
geoID.elementID = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
mapTable.isBatchDrawBroadWise = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
int geoDataSize = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
geoID.type = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
int ptsSize = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTSTR)
return mapTable;
geoID.name = pBuf->resval.rstring;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTSTR)
return mapTable;
geoID.desc = pBuf->resval.rstring;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
geoID.color = pBuf->resval.rlong;
// 读取vector<wstring>数据
for (int i = 0; i < exDataSize; i++)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTSTR)
return mapTable;
geoID.exDataVec.push_back(pBuf->resval.rstring);
}
mapTable.geoData = new tagGeometryWithID(geoID);
}
// 读取vector<shared_ptr<CBaseGeometry>>数据
if (pBuf->restype == RTSHORT && pBuf->resval.rint == 1)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
int ptrgeosSize = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
int type = pBuf->resval.rlong;
vector<AcGePoint3d> pts;
int ptsSize = 0;
string name, desc;
int color = 0;
// 读取vector<AcGePoint3d>数据
for (int i = 0; i < ptsSize; i++)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTPOINT)
return mapTable;
pts.push_back(asPnt3d(pBuf));
}
// 读取字符串、int等数据
while (true)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr)
break;
if (pBuf->restype == RTSTR)
{
if (name.empty())
name = pBuf->resval.rstring;
else if (desc.empty())
desc = pBuf->resval.rstring;
}
else if (pBuf->restype == RTLONG)
{
if (color == 0)
color = pBuf->resval.rlong;
}
}
// 根据type创建不同的子类对象
shared_ptr<CBaseGeometry> pGeo;
switch (type)
{
case 1:
pGeo = make_shared<CLine>(pts, name, desc, color);
break;
case 2:
pGeo = make_shared<CArc>(pts, name, desc, color);
break;
// 其他子类
}
mapTable.geoData->ptrgeos.push_back(pGeo);
}
return mapTable;
}
```
需要注意的是,保存和读取数据时,需要将数据按照一定的格式打包成resbuf链表,以便于保存到Xrecord中或从Xrecord中读取。对于不同类型的数据,需要使用不同的resbuf类型来保存和读取。具体的格式可以根据实际需要进行调整。