// SAVE WITHOUT DIALOG POPUP IF POSSIBLE public static void doSave_MenuItem(object obj) { //Debug.Log("LastLibraryPath="+EditorPrefs.GetString("LastLibraryPath")); //EditorPrefs.SetString("LastLibraryPath", ""); // 1. Is there a folder pref? string absoluteFolderPath = EditorPrefs.GetString("LastLibraryPath"); // of the form "Assets/folder1/folder2..." if (string.IsNullOrEmpty(absoluteFolderPath)) { // 1. THERE IS NO PREFERENCE FOR A FOLDER -- USE DEFAULT absoluteFolderPath = System.IO.Path.Combine(Application.dataPath, "ArchimatixLibrary"); //Debug.Log("CASE #1: " + absoluteFolderPath); doSavePO_ToSpecifiedFolder(assertDefaultLibraryFolder(), obj); } else if (!System.IO.Directory.Exists(absoluteFolderPath)) { // 2. THERE WAS A PREFERENCE, BUT IT DOES NOT EXIST -- Has the project name changed? string relativeFolderPath = ArchimatixUtils.getRelativeFilePath(absoluteFolderPath); string tryThisAbsolutePath = System.IO.Path.Combine(Application.dataPath, relativeFolderPath); //Debug.Log("CASE #2: " + tryThisAbsolutePath); if (System.IO.Directory.Exists(tryThisAbsolutePath)) { // 2a. There is an analagous folder in this project. go to file save panel with this as a suggestion doSavePO_ToSpecifiedFolder(tryThisAbsolutePath, obj); } else { // 2b. Go to file panel with default folder AXParametricObject po = (AXParametricObject)obj; doSave_SaveFilePanel(System.IO.Path.Combine(Application.dataPath, "ArchimatixLibrary"), po.Name, obj); } } else { //Debug.Log("CASE #3: " + absoluteFolderPath); // 3. The pref folder checks out, silently save to it... doSavePO_ToSpecifiedFolder(absoluteFolderPath, obj); } }
public static void removeLibraryItem(LibraryItem li) { string filepath = ArchimatixUtils.getAbsoluteLibraryPath(li.readIntoLibraryFromRelativeAXOBJPath); // First remove the item from the live library. THat way when the file is deleted, AX will not try to rebuild the library. //string nodeName = System.IO.Path.GetFileNameWithoutExtension(filepath); //LibraryItem li = ArchimatixEngine.library.getLibraryItem(nodeName); ArchimatixEngine.library.removeLibraryItemFromList(li); //string localFilepath = filepath.Substring(filepath.IndexOf("Assets/")); string axobjRelativePath = ArchimatixUtils.getRelativeFilePath(filepath);; // [Darkwell: change "undoable" to "cannot be undone" - 2016.06.02] if (EditorUtility.DisplayDialog("Delete Library Object?", "Are you sure you want to delete " + li.Name + "? This cannot be undone!", "Really Delete", "Cancel")) { AssetDatabase.DeleteAsset(axobjRelativePath); string prefix = (li.is2D) ? "zz-AX-2DLib-" : "zz-AX-3DLib-"; string filename = System.IO.Path.GetFileNameWithoutExtension(li.readIntoLibraryFromRelativeAXOBJPath); string noPrefixThumbnailPath = System.IO.Path.ChangeExtension(li.readIntoLibraryFromRelativeAXOBJPath, ".jpg"); string ThumbnailPathWithPrefix = noPrefixThumbnailPath.Replace(filename, prefix + filename); AssetDatabase.DeleteAsset(ThumbnailPathWithPrefix); //Debug.Log(thumbnailRelativePath + " deleted"); // Refresh the AssetDatabase after all the changes AssetDatabase.Refresh(); //library.readLibraryFromFiles(); } }
/* READ_LIBRARY FROM FILES * If the cache is there, use it. * Otherwise... * Parse all the files in the Library data folder * and, using the head object, extract meta data and build indices. */ public void readLibraryFromFiles() { //Debug.Log("read library"); //StopWatch sw = new StopWatch(); /* Maintain a live list of all the files found * as head POs * */ parametricObjects = new List <AXParametricObject>(); libraryItems = new List <LibraryItem>(); DirectoryInfo info = new DirectoryInfo(Application.dataPath); FileInfo[] files = info.GetFiles("*.axobj", SearchOption.AllDirectories); //Debug.Log ("LIBRARY READ " + files.Length); try { //foreach (var filepathname in files) for (int i = 0; i < files.Length; i++) { //Debug.Log ("file: " + files[i].ToString()); string filepathname = files[i].ToString(); AX.SimpleJSON.JSONNode jn = AX.SimpleJSON.JSON.Parse(File.ReadAllText(filepathname)); AXParametricObject head_po = null; if (jn ["parametric_objects"] != null) { head_po = JSONSerializersAX.ParametricObjectFromJSON(jn ["parametric_objects"] [0]); } else { head_po = JSONSerializersAX.ParametricObjectFromJSON(jn [0]); } //head_po.readIntoLibraryFromPathname = filepathname; head_po.readIntoLibraryFromRelativeAXOBJPath = ArchimatixUtils.getRelativeFilePath(filepathname); parametricObjects.Add(head_po); libraryItems.Add(new LibraryItem(head_po)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } rebuildTagListsFromExistingPOs(); cacheLibraryItems(); //Debug.Log ("LIBRARY READ IN "+sw.stop()+" MILLISECONDS " + parametricObjects.Count + " items."); }
// DO THE ACTUAL SAVE public static void saveParametricObject(AXParametricObject po, bool withInputSubparts, string filepathname) { //Debug.Log(filepathname); //EditorUtility.DisplayDialog("Archimatix Library", "Saving to Library: This may take a few moments.", "Ok"); po.readIntoLibraryFromRelativeAXOBJPath = ArchimatixUtils.getRelativeFilePath(filepathname); Library.recentSaveFolder = System.IO.Path.GetDirectoryName(filepathname); // If this head po has a grouperKey, lose it! // This is because when it is later instantiated, // that grouper may not exist or may not be the desired place for this. po.grouperKey = null; // gather relations as you go... List <AXRelation> rels = new List <AXRelation>(); // BUILD JSON STRING StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"parametric_objects\":["); sb.Append(JSONSerializersAX.ParametricObjectAsJSON(po)); // gather rels foreach (AXParameter p in po.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } // SUB NODES if (withInputSubparts) { // GATHER SUB_NODES //Debug.Log("Start gather"); po.gatherSubnodes(); //Debug.Log("End gather"); foreach (AXParametricObject spo in po.subnodes) { //Debug.Log("-- " + spo.Name); sb.Append(',' + JSONSerializersAX.ParametricObjectAsJSON(spo)); // gather rels foreach (AXParameter p in spo.parameters) { foreach (AXRelation rr in p.relations) { if (!rels.Contains(rr)) { rels.Add(rr); } } } } } sb.Append("]"); // add relations to json string thecomma; // RELATIONS if (rels != null && rels.Count > 0) { sb.Append(", \"relations\": ["); // begin parametric_objects thecomma = ""; foreach (AXRelation rr in rels) { sb.Append(thecomma + rr.asJSON()); thecomma = ", "; } sb.Append("]"); } sb.Append("}"); // *** SAVE AS ASSET *** // Since we have added the newItem to the live library, // generating this new file will not force a recreation of the library from the raw JSON file. File.WriteAllText(filepathname, sb.ToString()); // THUMBNAIL TO PNG if (po.is2D()) { Library.cache2DThumbnail(po); } else { Thumbnail.BeginRender(); Thumbnail.render(po, true); Thumbnail.EndRender(); } //string thumb_filename = System.IO.Path.ChangeExtension(filepathname, ".png"); string filename = System.IO.Path.GetFileNameWithoutExtension(filepathname); string prefix = (po.is2D()) ? "zz-AX-2DLib-" : "zz-AX-3DLib-"; string thumb_filename = System.IO.Path.ChangeExtension(filepathname, ".jpg"); thumb_filename = thumb_filename.Replace(filename, prefix + filename); byte[] bytes = po.thumbnail.EncodeToJPG(); //File.WriteAllBytes(libraryFolder + "data/"+ po.Name + ".png", bytes); File.WriteAllBytes(thumb_filename, bytes); //AssetDatabase.Refresh(); string thumbnailRelativePath = ArchimatixUtils.getRelativeFilePath(thumb_filename); //Debug.Log(path); AssetDatabase.ImportAsset(thumbnailRelativePath); TextureImporter importer = AssetImporter.GetAtPath(thumbnailRelativePath) as TextureImporter; if (importer != null) { importer.textureType = TextureImporterType.GUI; importer.maxTextureSize = 256; #if UNITY_5_5_OR_NEWER importer.textureCompression = TextureImporterCompression.Uncompressed; #else importer.textureFormat = TextureImporterFormat.AutomaticTruecolor; #endif } AssetDatabase.WriteImportSettingsIfDirty(thumbnailRelativePath); // Save the recent path to prefs EditorPrefs.SetString("LastLibraryPath", System.IO.Path.GetDirectoryName(filepathname)); EditorUtility.DisplayDialog("Archimatix Library", "ParametricObject saved to " + System.IO.Path.GetDirectoryName(thumbnailRelativePath) + " as " + filename, "Great, thanks!"); Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(thumbnailRelativePath, typeof(Texture2D)); LibraryItem newItem = new LibraryItem(po); newItem.icon = tex; ArchimatixEngine.library.addLibraryItemToList(newItem); ArchimatixEngine.library.sortLibraryItems(); ArchimatixEngine.saveLibrary(); AssetDatabase.Refresh(); //Debug.Log("yo 2"); //ArchimatixEngine.library.readLibraryFromFiles(); }