1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4 	This file is part of COLLADAMax.
5 
6     Portions of the code are:
7     Copyright (c) 2005-2007 Feeling Software Inc.
8     Copyright (c) 2005-2007 Sony Computer Entertainment America
9 
10     Based on the 3dsMax COLLADASW Tools:
11     Copyright (c) 2005-2006 Autodesk Media Entertainment
12 
13     Licensed under the MIT Open Source License,
14     for details please see LICENSE file or the website
15     http://www.opensource.org/licenses/mit-license.php
16 */
17 
18 
19 #include "COLLADAMaxStableHeaders.h"
20 
21 #include "COLLADAMaxXRefFunctions.h"
22 
23 #include <max.h>
24 #include "COLLADAMaxXRefIncludes.h"
25 
26 namespace COLLADAMax
27 {
28 
29 	//---------------------------------------------------------------
isXRefObject(Animatable * object)30 	bool XRefFunctions::isXRefObject(Animatable* object)
31 	{
32 #ifdef MAX_8_OR_NEWER
33 		return IXRefObject8::Is_IXRefObject8(* object);
34 #else
35 		return (object->SuperClassID() == SYSTEM_CLASS_ID && object->ClassID().PartA() == XREFOBJ_CLASS_ID);
36 #endif
37 	}
38 
39 
40 	//---------------------------------------------------------------
isXRefItem(Animatable * object)41 	bool XRefFunctions::isXRefItem(Animatable* object)
42 	{
43 #ifdef MAX_8_OR_NEWER
44 		return IXRefItem::IsIXRefItem(* object);
45 #else
46 		// no xref items in max7
47 		return false;
48 #endif
49 	}
50 
51 
52 	//---------------------------------------------------------------
isXRefMaterial(Animatable * material)53 	bool XRefFunctions::isXRefMaterial(Animatable* material)
54 	{
55 #ifdef MAX_8_OR_NEWER
56 		return IXRefMaterial::Is_IXRefMaterial(*material);
57 #else
58 		// not supported
59 		(void)material;
60 		return false;
61 #endif
62 	}
63 
64 #if 0
65 	//---------------------------------------------------------------
66 	fstring XRefFunctions::GetURL(Animatable* xref)
67 	{
68 		if (!IsXRefObject(xref) && !IsXRefMaterial(xref)) return FS("");
69 
70 		fstring maxUrl;
71 #ifdef USE_MAX_8
72 		{
73 			IXRefItem* x = IXRefItem::GetInterface(*xref);
74 			maxUrl = (x != NULL) ? x->GetSrcFileName() : emptyFString;
75 		}
76 #elif USE_MAX_7
77 		{
78 			IXRefObject* x = (IXRefObject*) xref;
79 			maxUrl = TO_FSTRING(x->GetFileName(FALSE).data());
80 		}
81 #endif
82 
83 		size_t xrefTokenIndex = maxUrl.find(FC(".xreffile"));
84 		if (xrefTokenIndex != fstring::npos)
85 		{
86 			// Remove the ".xreffile.max" suffix. The old extension should still be there.
87 			maxUrl.erase(xrefTokenIndex);
88 		}
89 
90 		// Replace the extension by .dae
91 		return maxUrl;
92 	}
93 #endif
94 
95 #if 0
96 	//---------------------------------------------------------------
97 	bool XRefFunctions::IsXRefCOLLADASW(Animatable* xref)
98 	{
99 		// Not performant, but the simplest method is to look at the extension of the filename.
100 		FUUri uri(XRefFunctions::GetURL(xref));
101 		fstring extension = FUFileManager::GetFileExtension(uri.GetPath());
102 		return IsEquivalentI(extension, FC("dae")) || IsEquivalentI(extension, FC("xml"));
103 	}
104 #endif
105 
106 #if 0
107 	//---------------------------------------------------------------
108 	fm::string XRefFunctions::GetSourceName(Animatable* xref)
109 	{
110 #if USE_MAX_8
111 		IXRefItem* x = IXRefItem::GetInterface(* xref);
112 		if (x == NULL)
113 			return "";
114 
115 		return fm::string(x->GetSrcItemName());
116 
117 #elif USE_MAX_7
118 		if (IsXRefObject(xref))
119 		{
120 			IXRefObject* xobj = (IXRefObject*)xref;
121 			return fm::string(xobj->GetObjName(FALSE).data());
122 		}
123 		else
124 		{
125 			return "(not xref)";
126 		}
127 #endif
128 	}
129 #endif
130 
131 	//---------------------------------------------------------------
getXRefObjectSource(Object * xRefObject,Tab<Modifier * > * modifiers)132 	Object* XRefFunctions::getXRefObjectSource(Object* xRefObject, Tab< Modifier * > * modifiers)
133 	{
134 		if ( !isXRefObject(xRefObject) )
135 			return xRefObject; // this is the source
136 #ifdef MAX_8_OR_NEWER
137 
138 		IXRefObject8* xRefObjectInterface = IXRefObject8::GetInterface(* xRefObject);
139 		if ( !xRefObjectInterface )
140 			return 0;
141 
142 		// resolve nested
143 		Object* source = xRefObjectInterface->GetSourceObject(false, modifiers);
144 
145 #else
146 		XRefObject_REDEFINITION* xobj = (XRefObject_REDEFINITION*)xRefObject;
147 		Object* source = xobj->obj;
148 
149 #endif
150 		return source;
151 	}
152 
153 
154 	//---------------------------------------------------------------
getXRefItemSource(Object * xRefObject)155 	Object* XRefFunctions::getXRefItemSource(Object* xRefObject)
156 	{
157 		if ( !isXRefItem(xRefObject) )
158 			return (IDerivedObject*) xRefObject; // this is the source
159 #ifdef MAX_8_OR_NEWER
160 
161 		IXRefItem * xRefObjectInterface = IXRefItem::GetInterface(* xRefObject);
162 		if ( !xRefObjectInterface )
163 			return 0;
164 
165 		// resolve nested
166 		IDerivedObject* source = (IDerivedObject*) xRefObjectInterface->GetSrcItem(false);
167 
168 		INode* bb1 = source->GetWorldSpaceObjectNode();
169 
170 
171 		int h1h = 5;
172 
173 #else
174 		// no xref items in max7
175 		Object* source = xRefObject;
176 #endif
177 		return source;
178 	}
179 
180 
181 
182 #if 0
183 	//---------------------------------------------------------------
184 	fm::string XRefFunctions::GetXRefObjectName(Object* xref)
185 	{
186 		if (!IsXRefObject(xref)) return emptyString; // this is the source
187 #if USE_MAX_8
188 
189 		IXRefObject8* xobj = IXRefObject8::GetInterface(*xref);
190 		if (xobj == NULL) return emptyString;
191 		IXRefItem* item = IXRefItem::GetInterface(*xref);
192 		if (item == NULL) return emptyString;
193 		return item->GetSrcItemName();
194 #elif USE_MAX_7
195 
196 		XRefObject_REDEFINITION* xobj = (XRefObject_REDEFINITION*)xref;
197 		return xobj->xrefObj.data();
198 #endif
199 	}
200 #endif
201 
202 	//---------------------------------------------------------------
getXRefMaterialSource(Mtl * xRefMaterial)203 	Mtl* XRefFunctions::getXRefMaterialSource(Mtl* xRefMaterial)
204 	{
205 #ifdef MAX_8_OR_NEWER
206 
207 		if (!isXRefMaterial(xRefMaterial))
208 		{
209 			// this is the source
210 			return xRefMaterial;
211 		}
212 
213 		IXRefMaterial* xRefMaterialInterface = IXRefMaterial::GetInterface(* xRefMaterial);
214 		if ( !xRefMaterialInterface )
215 			return 0;
216 
217 		// resolve nested
218 		Mtl* source = xRefMaterialInterface->GetSourceMaterial(true);
219 		return source;
220 
221 #else
222 		// not supported in Max 7
223 		return xRefMaterial;
224 #endif
225 	}
226 
227 }
228