void ApplinkDialog::Timer(const BaseContainer &msg)
{
	Bool b;
	GetBool(IDC_CHK_PROMPT, b);
	if(b)
	{
		Filename fn;
		fn.SetDirectory(gPreferences.GetString(IDC_EXCH_FOLDER));
		fn.SetFile("export.txt");
        
		if(GeFExist(fn))
		{
			GePrint("File exists!");
			SetTimer(0);
			if(GeOutString("To import a new object?", GEMB_OKCANCEL) == GEMB_R_OK)
			{
				GePrint("Start import!");
				BaseDocument* doc = GetActiveDocument();
				ApplinkImporter* importer = NewObjClear(ApplinkImporter);
				importer->Execute(doc, &gPreferences);
				SetTimer(1000);
			}
			else
			{
				SetBool(IDC_CHK_PROMPT, false);
			}
		}
	}
}
Bool ApplinkExporter::WriteMatsFile(BaseDocument* document, BaseContainer* bc)
{
	Filename filenameMTL;
	filenameMTL.SetDirectory(bc->GetString(IDC_TMP_FOLDER));
	filenameMTL.SetFile(document->GetDocumentName());
	filenameMTL.SetSuffix("mtl");
	
	GePrint(filenameMTL.GetString());

	AutoAlloc<BaseFile> fileMTL;
		
	if (!fileMTL->Open(filenameMTL, FILEOPEN_WRITE, FILEDIALOG_NONE, GeGetByteOrder())) return FALSE;
	
	for(LONG i=0; i < materialArray.GetCount(); i++)
	{
		BaseMaterial* mat = materialArray[i];
		
		String str;
		str = "newmtl " + mat->GetName() + "\n";
		this->WriteString(str, fileMTL);

		//Ka
		str = "Ka 0.300000 0.300000 0.300000\n";
		this->WriteString(str, fileMTL);

/////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
		//Kd
		if(getParameterLong(*mat, MATERIAL_USE_COLOR))
		{
			ExportChannel(document, fileMTL, *mat, MATERIAL_COLOR_SHADER, MATERIAL_COLOR_COLOR, "Kd");
		}

		//Ks
		if(getParameterLong(*mat, MATERIAL_USE_REFLECTION))
		{
			ExportChannel(document, fileMTL, *mat, MATERIAL_REFLECTION_SHADER, MATERIAL_REFLECTION_COLOR, "Ks");
		}

		//Ns
		str = "Ns 50.000000\n";
		this->WriteString(str, fileMTL);

		//Tr
		str = "Tr 0.000000\n";
		this->WriteString(str, fileMTL);

		//illum
		str = "illum 2\n";
		this->WriteString(str, fileMTL);
		this->WriteEndLine(fileMTL);
	}

	fileMTL->Close();

	return TRUE;
}
Bool ApplinkExporter::Execute(BaseDocument* document, BaseContainer* bc)
{
	matDefault = BaseMaterial::Alloc(Mmaterial);
	if(!matDefault) return false;

	Filename fileObjPath;
	fileObjPath.SetDirectory(bc->GetString(IDC_TMP_FOLDER));
	fileObjPath.SetFile(document->GetDocumentName());
	fileObjPath.SetSuffix("obj");
	Filename fileObjOutPath;
	fileObjOutPath.SetDirectory(bc->GetString(IDC_TMP_FOLDER));
	fileObjOutPath.SetFile("output.obj");
	Filename fileImport;
	fileImport.SetDirectory(bc->GetString(IDC_EXCH_FOLDER));
	fileImport.SetFile("import.txt");

	GePrint(fileObjPath.GetString());
	GePrint(fileObjOutPath.GetString());
	GePrint(fileImport.GetString());

	const Matrix tM(LVector(0.0f, 0.0f, 0.0f), LVector(1.0f, 0.0f, 0.0f), LVector(0.0f, 1.0f, 0.0f), LVector(0.0f, 0.0f, -1.0f));

	//BaseDocument* doc = document->Polygonize();
	AutoAlloc<AtomArray> oSel;
	document->GetActiveObjects(oSel, GETACTIVEOBJECTFLAGS_0);

	if(oSel->GetCount() > 0)
	{
//Write import.txt//
		AutoAlloc<BaseFile> basefileImport;
		
		if (!basefileImport->Open(fileImport, FILEOPEN_WRITE, FILEDIALOG_NONE, GeGetByteOrder())) return FALSE;
		
		this->WriteString(fileObjPath.GetString() + "\n", basefileImport);
		this->WriteString(fileObjOutPath.GetString() + "\n", basefileImport);
		this->WriteString(mapType(bc->GetLong(IDC_COMBO_MAP_TYPE)) + "\n", basefileImport);

		Bool bSkipImp = bc->GetBool(IDC_CHK_SKIP_IMP_DIALOG);

		if(bSkipImp)
		{
			this->WriteString("[SkipImport]\n", basefileImport);
		}

		Bool bSkipExp = bc->GetBool(IDC_CHK_SKIP_EXP_DIALOG);

		if(bSkipExp)
		{
			this->WriteString("[SkipExport]\n", basefileImport);
		}

		GePrint(mapType(bc->GetLong(IDC_COMBO_MAP_TYPE)));
		basefileImport->Close();

		GePrint("File " + fileImport.GetString() + " write success!");

//Write file.obj//
		AutoAlloc<BaseFile> objfile;

		//if (!objfile) return FALSE;
		if (!objfile->Open(fileObjPath, FILEOPEN_WRITE, FILEDIALOG_NONE, GeGetByteOrder())) return FALSE;

		String str;
		str = "#Wavefront OBJ Export for 3D-Coat\n";
		this->WriteString(str, objfile);
		DateTime t;
		GetDateTimeNow(t);
		str = "#File created: " + FormatTime("%d.%m.%Y  %H:%M:%S", t) + "\n";
		this->WriteString(str, objfile);
		str = "#Cinema4D Version: " + LongToString(GetC4DVersion()) + "\n";
		this->WriteString(str, objfile);
		this->WriteEndLine(objfile);

		Bool expMat = bc->GetBool(IDC_CHK_EXP_MAT);
		vpcnt = vtcnt = 0;

		for(int i = 0; i < oSel->GetCount(); i++)
		{
			StatusSetSpin();
			PolygonObject* ob = (PolygonObject*) oSel->GetIndex(i);
			if (ob->GetType() == Opolygon)
			{
				StatusSetText("Export object " + ob->GetName());
				ExportObject mObject;

				GePrint("Name " + ob->GetName());
				//GePrint("Type " + LongToString(ob->GetType()));

				if(expMat)
				{
					mObject.pmatidxArray.ReSize(ob->GetPolygonCount());
					mObject.tempMats.ReSize(1);
					mObject.pmatidxArray.Fill(0);
					Bool haveMats = false;
	//////////////////////////////////////////
					for(BaseTag* tag = ob->GetFirstTag(); tag != NULL; tag = tag->GetNext())
					{
						LONG typ = tag->GetType();
						if(typ == Ttexture)
						{
							if (!getParameterLink(*tag, TEXTURETAG_MATERIAL, Mbase)) continue;
						
							haveMats = true;
							TextureTag* txttag = (TextureTag*)tag;
							BaseMaterial* material = txttag->GetMaterial();

							if(material == NULL)
							{
								GePrint("Material not found on " + ob->GetName() + "object.");
								return false;
							}
							//GePrint("Mat Name: " + material->GetName());						

							String restrict = getParameterString(*tag, TEXTURETAG_RESTRICTION);
							if (restrict.Content())
							{
								mObject.tempMats.Push(material);
								//GePrint("Selection: " + restrict);
								for(BaseTag* seltag = ob->GetFirstTag(); seltag != NULL; seltag = seltag->GetNext())
								{
									LONG seltyp = seltag->GetType();
									if(seltyp == Tpolygonselection && seltag->GetName() == restrict)
									{
										SelectionTag* selecttag = (SelectionTag*)seltag;
										BaseSelect* sel = selecttag->GetBaseSelect();
										//GePrint("sel data count: " + LongToString(sel->GetCount()));

										LONG seg = 0, a, b, p;
										while (sel->GetRange(seg++, &a, &b))
										{
											for (p = a; p <= b; ++p)
											{
												//GePrint("seltpolygon: " + LongToString(p));
												mObject.pmatidxArray[p] = mObject.tempMats.GetCount()-1;
											}
										}
									}
								}
							}
							else
							{
								mObject.tempMats[0] = material;
								mObject.pmatidxArray.Fill(0);
							}
						}
					}

					if(!mObject.tempMats[0])
					{
						matDefault->SetName("Default");

						BaseChannel* color = matDefault->GetChannel(CHANNEL_COLOR);
						if (!color) return false;	// return some error
						BaseContainer cdata = color->GetData();
						cdata.SetVector(BASECHANNEL_COLOR_EX, Vector(1.0f, 1.0f, 1.0f));
						
						//document->InsertMaterial(matDefault, NULL, FALSE);
						//matDefault->Update(TRUE, TRUE);
						//matDefault->Message(MSG_UPDATE);

						mObject.tempMats[0] = matDefault;
						//GePrint("Global material not found on object " + ob->GetName() + ".");
						//return false;
					}

					if(haveMats)
					{
						//GePrint("mObject.tempMats.GetCount(): " + LongToString(mObject.tempMats.GetCount()));
						for(LONG m = 0; m < mObject.tempMats.GetCount(); m++)
						{
							Bool inMats = false;
							//GePrint("materialArray.GetCount(): " + LongToString(materialArray.GetCount()));
							for(LONG n = 0; n < materialArray.GetCount(); n++)
							{
								if(mObject.tempMats[m]->GetName() == materialArray[n]->GetName())
								{
									inMats = true;
									break;
								}
							}
							if(!inMats)
							{
								materialArray.Push(mObject.tempMats[m]);
							}
						}
					}

					//String str1;
					//for (LONG p = 0; p < ob->GetPolygonCount(); p++)
					//{
					//	str1 += LongToString(mObject.pmatidxArray[p]) + ",";
					//}
					//GePrint(str1);
				}
/////////////////////////////////////////////////
				const Vector* vadr = ob->GetPointR();
				const CPolygon* padr = ob->GetPolygonR();
				LONG vcnt = ob->GetPointCount();
				LONG pcnt = ob->GetPolygonCount();

				mObject.Fpvnb.ReSize(pcnt);// poly counts
				for(LONG p = 0; p < pcnt; p++)
				{
					if(padr[p].c != padr[p].d)
					{
						mObject.Fpvnb[p] = 4;
					}
					else
					{
						mObject.Fpvnb[p] = 3;
					}
				}
				mObject.pVertexCount = PVertexLength(mObject);

				//Vertex positions
				mObject.Vp.ReSize(vcnt);
				Matrix mg = tM * ob->GetMgn();
				for (LONG v = 0; v < vcnt; v++)
				{
					mObject.Vp[v] = vadr[v] * mg;
					//GePrint("Point[" + LongToString(i) + "] " + LongToString(padr[i].x) + ", " + LongToString(padr[i].y) + ", " + LongToString(padr[i].z));
					//str = "v " + LongToString(vadr[p].x) + " " + LongToString(vadr[p].y) + " " + LongToString(vadr[p].z) + "\n";
					//this->WriteString(str, objfile);
				}
				
				mObject.Fv.ReSize(mObject.pVertexCount);
				LONG y=0;
				for (LONG p = 0; p < pcnt; p++)
				{
					if(mObject.Fpvnb[p] == 4)
					{
						mObject.Fv[y] = padr[p].d;
						mObject.Fv[y+1] = padr[p].c;
						mObject.Fv[y+2] = padr[p].b;
						mObject.Fv[y+3] = padr[p].a;
					}
					else
					{
						mObject.Fv[y] = padr[p].c;
						mObject.Fv[y+1] = padr[p].b;
						mObject.Fv[y+2] = padr[p].a;
					}

					y += mObject.Fpvnb[p];
				}
				
				//String str1;
				//for (LONG p = 0; p < mObject.Fv.GetCount(); p++)
				//{
				//	str1 += LongToString(mObject.Fv[p]) + " ";
				//}
				//GePrint(str1);
///////////////////////////////
///////////vertex UV
//////////////////////////////
				if(bc->GetBool(IDC_CHK_EXP_UV))
				{
					// Get first UV tag (if at least one)
					UVWTag* uvw_tag = (UVWTag*)ob->GetTag(Tuvw, 0);
					if(!uvw_tag)
					{
						GePrint("Object \"" + ob->GetName() + "\" has no UVW tag.\nUV coordinates can't be exported.");
						return FALSE;
					}
					else
					{
						mObject.Vt.ReSize(mObject.pVertexCount);
						mObject.Fvt.ReSize(mObject.pVertexCount);						
						const UVWHandle dataptr = uvw_tag->GetDataAddressR();
						UVWStruct res;
						
						for(LONG t=0, y=0; t < pcnt; t++)
						{
							//GePrint("y: " + LongToString(y));
							UVWTag::Get(dataptr, t, res);
							if(mObject.Fpvnb[t] == 4)
							{
								mObject.Vt[y] = res.d;
								mObject.Vt[y + 1] = res.c;
								mObject.Vt[y + 2] = res.b;
								mObject.Vt[y + 3] = res.a;
							
								mObject.Fvt[y] = y;
								mObject.Fvt[y + 1] = y + 1;
								mObject.Fvt[y + 2] = y + 2;
								mObject.Fvt[y + 3] = y + 3;

							}
							else
							{
								mObject.Vt[y] = res.c;
								mObject.Vt[y + 1] = res.b;
								mObject.Vt[y + 2] = res.a;

								mObject.Fvt[y] = y;
								mObject.Fvt[y + 1] = y + 1;
								mObject.Fvt[y + 2] = y + 2;

							}
							y += mObject.Fpvnb[t];
						}
					}
					//String str1;
					//for (LONG p = 0; p < mObject.Fvt.GetCount(); p++)
					//{
					//	str1 += LongToString(mObject.Fvt[p]) + " ";
					//}
					//GePrint(str1);

				}

				WriteExportFile(bc, ob, objfile, mObject, vcnt, pcnt);
				//GePrint("Fvt: " + LongToString(Fvt.GetCount()));
				vpcnt += mObject.Vp.GetCount();
				if(bc->GetBool(IDC_CHK_EXP_UV))
					vtcnt += mObject.Vt.GetCount();
			}
		}
		objfile->Close();

		if(expMat && materialArray.GetCount() > 0)
			WriteMatsFile(document, bc);
	}
	else
	{
		GePrint("No selected objects!");
	}

	BaseMaterial::Free(matDefault);
	return TRUE;
}
void ApplinkExporter::WriteExportFile(BaseContainer* bc, PolygonObject* ob, BaseFile* objfile, ExportObject& mObject, LONG vcnt, LONG pcnt)
{
	const CPolygon* padr = ob->GetPolygonR();
	Bool expUV = bc->GetBool(IDC_CHK_EXP_UV);
	Bool expMat = bc->GetBool(IDC_CHK_EXP_MAT);
	String str;

	if(expMat && materialArray.GetCount() > 0)
	{
		Filename fileMatObj;
		fileMatObj.SetFile(GetActiveDocument()->GetDocumentName());
		fileMatObj.SetSuffix("mtl");

		str = "mtllib " + fileMatObj.GetFile().GetString() + "\n";
		this->WriteString(str, objfile);
		this->WriteEndLine(objfile);
	}

	str = "g " + ob->GetName() + "\n";
	this->WriteString(str, objfile);
	this->WriteEndLine(objfile);

	// vertex positions
	ApplinkExporter::WriteVertexPositions(objfile, mObject, vcnt);
	//UV
	if(expUV)
		ApplinkExporter::WriteUVWTag(objfile, mObject, pcnt, padr);

	//Polygon faces v/vt/vn (v//vn)
	str = "# begin " + LongToString(pcnt) + " faces\n";
	this->WriteString(str, objfile);

	LONG y=0;
	String prevMat = "", currMat = "";

	for (LONG i = 0; i < pcnt; i++)
	{
		if(expMat && materialArray.GetCount() > 0)
		{
			currMat = mObject.tempMats[mObject.pmatidxArray[i]]->GetName();
			if(currMat != prevMat)
			{
				str = "usemtl " + currMat + "\n";
				this->WriteString(str, objfile);
				prevMat = currMat;
			}
		}

		//GePrint("Polygon[" + LongToString(i) + "] " + LongToString(vadr[i].a) + ", " + LongToString(vadr[i].b) + ", " + LongToString(vadr[i].c) + ", " + LongToString(vadr[i].d));
		str = "f";
		//GePrint("poly vertices: " + LongToString(mObject.Fpvnb[i]));
		for(LONG j = 0; j < mObject.Fpvnb[i]; j++)
		{			
			str += " ";
			str += LongToString(mObject.Fv[y+j] + 1 + vpcnt);

			if(expUV && mObject.Fvt.GetCount() > 0)
			{
				str += "/";
				str += LongToString(mObject.Fvt[y+j] + 1 + vtcnt);
			}
		}

		str += "\n";
		//GePrint("str = " + str);
		this->WriteString(str, objfile);
		y += mObject.Fpvnb[i];
	}

	str = "# end " + LongToString(pcnt) + " faces\n";
	this->WriteString(str, objfile);
	this->WriteEndLine(objfile);
}