Bool ApplinkImporter::createObjects()
{
	BaseDocument* doc = GetActiveDocument();
	Int32 vertCnt = 0;
	const Matrix tM(Vector(0.0f, 0.0f, 0.0f), Vector(1.0f, 0.0f, 0.0f), Vector(0.0f, 1.0f, 0.0f), Vector(0.0f, 0.0f, -1.0f));

	StatusSetText("Create objects...");
	for(unsigned int i = 0; i < groups.size(); i++)
	{
		Vector *vadr = 0;// vertex adress
		CPolygon *padr = 0;// polygon adress

		Group gr = this->groups[i];

		if(this->pSet.impReplace)
		{
			BaseObject* bo = doc->SearchObject(gr.groupName);
			if(bo)
			{				
				BaseObject::Free(bo);
			}
		}

		PolygonObject* pObj = PolygonObject::Alloc(gr.numGVertices, gr.numGFaces);
		if (!pObj){ return false;}

		pObj->SetName(this->groups[i].groupName);

		//GePrint("Name group: " + pObj->GetName());
		GePrint("V count: " + String::IntToString((Int32)gr.numGVertices));
		GePrint("Poly count: " + String::IntToString((Int32)gr.numGFaces));

		vadr = pObj->GetPointW();
		padr = pObj->GetPolygonW();

		for (int p = 0; p < gr.numGFaces; p++)
		{
			padr[p] = CPolygon(gr.faces[p].vp[0] - vertCnt, gr.faces[p].vp[1] - vertCnt, gr.faces[p].vp[2] - vertCnt, gr.faces[p].vp[3] - vertCnt);
			//GePrint("poly " + LongToString(p) + ": " + LongToString(padr[p].a) + ", " + LongToString(padr[p].b) + ", " + LongToString(padr[p].c) + ", " + LongToString(padr[p].d));
		}

		for (int v = 0; v < gr.numGVertices; v++)
		{
			vadr[v] = tM * this->verticies[v + vertCnt] ;
			//GePrint("vert " + LongToString(v) + ": " + LongToString(vadr[v].x) + ", " + LongToString(vadr[v].y) + ", " + LongToString(vadr[v].z));
		}
	
//// import UV
		if(ApplinkImporter::pSet.impUV && gr.numGTVertices > 0)
		{
			UVWStruct us;
			UVWTag* uvwtag = nullptr;

			uvwtag = UVWTag::Alloc(gr.numGFaces);
			if(!uvwtag) return false;

			for (Int32 p = 0; p < gr.numGFaces; p++)
			{
				us = UVWStruct(this->uvw[gr.faces[p].vt[0]], this->uvw[gr.faces[p].vt[1]], this->uvw[gr.faces[p].vt[2]], this->uvw[gr.faces[p].vt[3]]);
				void *dataptr = uvwtag->GetDataAddressW();
				uvwtag->Set(dataptr, p, us);
			}

			pObj->InsertTag(uvwtag, NULL);
			uvwtag->Message(MSG_UPDATE);
		}

////// insert phongTag
		if (!pObj->MakeTag(Tphong)) GePrint("Error on inserting phongTag. Object: " + pObj->GetName());

/////add materials, textures and poly clusters
		if(this->pSet.impMat)
		{
			String selTagName = "";
			if(this->matArray.size() == 1)
			{
				this->InsertTextureTag(pObj, this->matArray[0].Name, selTagName);
			}
			else if(this->matArray.size() > 1)
			{
				this->InsertTextureTag(pObj, this->matArray[0].Name, selTagName);

				CPolygon ps;
				SelectionTag* polyTag = NULL;

				for (Int32 c = 1; c < this->matArray.size(); c++)
				{
					polyTag = SelectionTag::Alloc(Tpolygonselection);
					if(!polyTag) return false;

					selTagName = "Selection " + String::IntToString(c);
					polyTag->SetName(selTagName);
					BaseSelect* sel = polyTag->GetBaseSelect();
					for (Int32 p = 0; p < gr.numGFaces; p++)
					{
						if(gr.polyMatIdx[p] == c)
						{
							sel->Select(p);
						}
					}
					
					pObj->InsertTag(polyTag, this->GetLastTag(pObj));
					polyTag->Message(MSG_UPDATE);

					this->InsertTextureTag(pObj, this->matArray[c].Name, selTagName);
				}
			}
		}

		doc->InsertObject(pObj, NULL, NULL);
		pObj->Message(MSG_UPDATE);

		ModelingCommandData md;
		md.doc = doc;
		md.op  = pObj;
		if(!SendModelingCommand(MCOMMAND_REVERSENORMALS, md)) return false;

		pObj = 0;
		vertCnt += gr.numGVertices;
	}

	return true;
}