Support saving groups recursively.

This commit is contained in:
leo
2014-09-13 06:48:51 +00:00
parent c0db665346
commit 6315ff0b55
2 changed files with 67 additions and 51 deletions
+8 -8
View File
@@ -118,14 +118,14 @@ void lcCamera::SaveLDraw(QTextStream& Stream) const
Stream << QLatin1String("0 !LEOCAD CAMERA POSITION ") << mPosition[0] << ' ' << mPosition[1] << ' ' << mPosition[2] << LineEnding;
if (mTargetPositionKeys.GetSize() > 1)
SaveKeysLDraw(Stream, mTargetPositionKeys, "CAMERA TARGETPOSITION_KEY ");
SaveKeysLDraw(Stream, mTargetPositionKeys, "CAMERA TARGET_POSITION_KEY ");
else
Stream << QLatin1String("0 !LEOCAD CAMERA TARGETPOSITION ") << mTargetPosition[0] << ' ' << mTargetPosition[1] << ' ' << mTargetPosition[2] << LineEnding;
Stream << QLatin1String("0 !LEOCAD CAMERA TARGET_POSITION ") << mTargetPosition[0] << ' ' << mTargetPosition[1] << ' ' << mTargetPosition[2] << LineEnding;
if (mUpVectorKeys.GetSize() > 1)
SaveKeysLDraw(Stream, mUpVectorKeys, "CAMERA UPVECTOR_KEY ");
SaveKeysLDraw(Stream, mUpVectorKeys, "CAMERA UP_VECTOR_KEY ");
else
Stream << QLatin1String("0 !LEOCAD CAMERA UPVECTOR ") << mUpVector[0] << ' ' << mUpVector[1] << ' ' << mUpVector[2] << LineEnding;
Stream << QLatin1String("0 !LEOCAD CAMERA UP_VECTOR ") << mUpVector[0] << ' ' << mUpVector[1] << ' ' << mUpVector[2] << LineEnding;
Stream << QLatin1String("0 !LEOCAD CAMERA ");
@@ -157,15 +157,15 @@ bool lcCamera::ParseLDrawLine(QTextStream& Stream)
Stream >> m_zFar;
else if (Token == QLatin1String("POSITION"))
Stream >> mPosition[0] >> mPosition[1] >> mPosition[2];
else if (Token == QLatin1String("TARGETPOSITION"))
else if (Token == QLatin1String("TARGET_POSITION"))
Stream >> mTargetPosition[0] >> mTargetPosition[1] >> mTargetPosition[2];
else if (Token == QLatin1String("UPVECTOR"))
else if (Token == QLatin1String("UP_VECTOR"))
Stream >> mUpVector[0] >> mUpVector[1] >> mUpVector[2];
else if (Token == QLatin1String("POSITION_KEY"))
LoadKeysLDraw(Stream, mPositionKeys);
else if (Token == QLatin1String("TARGETPOSITION_KEY"))
else if (Token == QLatin1String("TARGET_POSITION_KEY"))
LoadKeysLDraw(Stream, mTargetPositionKeys);
else if (Token == QLatin1String("UPVECTOR_KEY"))
else if (Token == QLatin1String("UP_VECTOR_KEY"))
LoadKeysLDraw(Stream, mUpVectorKeys);
else if (Token == QLatin1String("NAME"))
{
+59 -43
View File
@@ -156,61 +156,82 @@ void lcModel::SaveLDraw(QTextStream& Stream) const
if (mCurrentStep != LastStep)
Stream << QLatin1String("0 !LEOCAD MODEL CURRENT_STEP") << mCurrentStep << LineEnding;
lcGroup* CurrentGroup = NULL;
lcArray<lcGroup*> SavedGroups;
lcArray<lcGroup*> CurrentGroups;
for (lcStep Step = 1; Step <= LastStep; Step++)
{
for (int PieceIdx = 0; PieceIdx < mPieces.GetSize(); PieceIdx++)
{
lcPiece* Piece = mPieces[PieceIdx];
if (Piece->GetStepShow() != Step)
continue;
lcGroup* PieceGroup = Piece->GetGroup();
if (PieceGroup != CurrentGroup)
if (PieceGroup)
{
if (CurrentGroup)
Stream << QLatin1String("0 !LEOCAD GROUP END\r\n");
if (PieceGroup)
Stream << QLatin1String("0 !LEOCAD GROUP BEGIN ") << PieceGroup->m_strName << LineEnding;
if (PieceGroup->mGroup && SavedGroups.FindIndex(PieceGroup) == -1)
if (CurrentGroups.IsEmpty() || (!CurrentGroups.IsEmpty() && PieceGroup != CurrentGroups[CurrentGroups.GetSize() - 1]))
{
Stream << QLatin1String("0 !LEOCAD GROUP PARENT ") << PieceGroup->mGroup->m_strName << LineEnding;
SavedGroups.Add(PieceGroup);
}
lcArray<lcGroup*> PieceParents;
CurrentGroup = PieceGroup;
for (lcGroup* Group = PieceGroup; Group; Group = Group->mGroup)
PieceParents.InsertAt(0, Group);
int FoundParent = -1;
while (!CurrentGroups.IsEmpty())
{
lcGroup* Group = CurrentGroups[CurrentGroups.GetSize() - 1];
int Index = PieceParents.FindIndex(Group);
if (Index == -1)
{
CurrentGroups.RemoveIndex(CurrentGroups.GetSize() - 1);
Stream << QLatin1String("0 !LEOCAD GROUP END\r\n");
}
else
{
FoundParent = Index;
break;
}
}
for (int ParentIdx = FoundParent + 1; ParentIdx < PieceParents.GetSize(); ParentIdx++)
{
lcGroup* Group = PieceParents[ParentIdx];
CurrentGroups.Add(Group);
Stream << QLatin1String("0 !LEOCAD GROUP BEGIN ") << Group->m_strName << LineEnding;
}
}
}
else
{
while (CurrentGroups.GetSize())
{
CurrentGroups.RemoveIndex(CurrentGroups.GetSize() - 1);
Stream << QLatin1String("0 !LEOCAD GROUP END\r\n");
}
}
if (Piece->GetStepShow() == Step)
Piece->SaveLDraw(Stream);
Piece->SaveLDraw(Stream);
}
if (Step != LastStep)
Stream << QLatin1String("0 STEP\r\n");
}
if (CurrentGroup)
while (CurrentGroups.GetSize())
{
CurrentGroups.RemoveIndex(CurrentGroups.GetSize() - 1);
Stream << QLatin1String("0 !LEOCAD GROUP END\r\n");
}
for (int CameraIdx = 0; CameraIdx < mCameras.GetSize(); CameraIdx++)
mCameras[CameraIdx]->SaveLDraw(Stream);
for (int LightIdx = 0; LightIdx < mLights.GetSize(); LightIdx++)
mLights[LightIdx]->SaveLDraw(Stream);
for (int GroupIdx = 0; GroupIdx < mGroups.GetSize(); GroupIdx++)
{
lcGroup* Group = mGroups[GroupIdx];
if (Group->mGroup && SavedGroups.FindIndex(Group) == -1)
{
Stream << QLatin1String("0 !LEOCAD GROUP BEGIN ") << Group->m_strName << LineEnding;
Stream << QLatin1String("0 !LEOCAD GROUP PARENT ") << Group->mGroup->m_strName << LineEnding;
Stream << QLatin1String("0 !LEOCAD GROUP END\r\n");
}
}
}
void lcModel::LoadLDraw(const QStringList& Lines, const lcMatrix44& CurrentTransform, int& CurrentStep)
@@ -218,7 +239,7 @@ void lcModel::LoadLDraw(const QStringList& Lines, const lcMatrix44& CurrentTrans
lcPiece* Piece = NULL;
lcCamera* Camera = NULL;
lcLight* Light = NULL;
lcGroup* Group = NULL;
lcArray<lcGroup*> CurrentGroups;
for (int LineIdx = 0; LineIdx < Lines.size(); LineIdx++)
{
@@ -280,20 +301,15 @@ void lcModel::LoadLDraw(const QStringList& Lines, const lcMatrix44& CurrentTrans
{
QString Name = Stream.readAll().trimmed();
QByteArray NameUtf = Name.toUtf8(); // todo: replace with qstring
Group = GetGroup(NameUtf.constData(), true);
lcGroup* Group = GetGroup(NameUtf.constData(), true);
if (!CurrentGroups.IsEmpty())
Group->mGroup = CurrentGroups[CurrentGroups.GetSize() - 1];
CurrentGroups.Add(Group);
}
else if (Token == QLatin1String("END"))
{
Group = NULL;
}
else if (Token == QLatin1String("PARENT"))
{
if (Group)
{
QString Name = Stream.readAll().trimmed();
QByteArray NameUtf = Name.toUtf8(); // todo: replace with qstring
Group->mGroup = GetGroup(NameUtf.constData(), true);
}
if (!CurrentGroups.IsEmpty())
CurrentGroups.RemoveIndex(CurrentGroups.GetSize() - 1);
}
}
@@ -323,8 +339,8 @@ void lcModel::LoadLDraw(const QStringList& Lines, const lcMatrix44& CurrentTrans
if (!Piece)
Piece = new lcPiece(NULL);
if (Group)
Piece->SetGroup(Group);
if (!CurrentGroups.IsEmpty())
Piece->SetGroup(CurrentGroups[CurrentGroups.GetSize() - 1]);
PieceInfo* Info = lcGetPiecesLibrary()->FindPiece(PartID.toLatin1().constData(), false);
if (Info != NULL)