Getting undefined instance tree for DWFX file - autodesk-forge

We are trying to get all node elements of DWFX file but we are getting undefined instance tree for DWFX file. We have used below code to get each element id.
// Try to get instance tree for DWFX file
var model = this.viewer.model;
var modelData = model.getData();
var it = modelData.instanceTree; // get instance tree
We have used another way to get element node id for DWFX file. (In that case, we are getting only panel label id for DWFX file) But that logic is not working for all DWFX files.
// Try to get all ids for DWFX file
var model = this.viewer.model;
var modelData = model.getData();
var allIds = modelData.stringDbIds; // get all ids
Please us know If I am using wrong approach to get all elements for DWFX file.

You need to wait for Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT event to make sure the instanceTree is available in your loaded model:
viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, function () {
var model = this.viewer.model;
var modelData = model.getData();
var it = modelData.instanceTree;
console.log(it)
})
In some cases you may have to wait also for Autodesk.Viewing.GEOMETRY_LOADED_EVENT event if you intend to access geometry of the components. Here is an article that may be relevant: Asynchronous viewer events notification

Related

How do you POST files in chunks, to server, using input id as param, in HTML/jQuery?

Description
I have a function that takes HTTP_POST path for server side hardcoded .js handler and file id from input[type=file]. I'm trying to POST that binary file to server but in chunks with no success.
What have you tried?
Using my uncle wisdom, I have noted that there is a way to slice the file using :
var File1 = document.querySelector('input').files[0].slice(0,7);
But passing this variable to the function is not an option since it takes id ="file_1" as a parameter from <input type="file">. Also, printing this variable doesn't work with console.log (half of you are already thinking to send me back to uni) hint: I've never studied Javascript. So I poked my uncle a little harder and found it. FileReader gives you the ability to actually read the inside, which gave me hope since my file was printed out to console.log
var reader = new FileReader;
reader.readAsBinaryString(File1);
reader.onload = function(e) {
var rawLog = reader.result;
console.log(rawLog);
};
At this point console.log gave me a print of first seven bytes, of my file. And by all of this we got to the question I wish to ask
Question ?¿
How to past this print as with input to the function so I can have this file sliced and saved?
You can slice off a piece of the file, use that piece to create a File object add it to a FileList and then overwrite the FileList from the input with the one that has your new file
var input = document.querySelector('input');
var originalfile = input.files[0];
var slice = originalfile.slice(0,7); // slice the file
var newfile = new File([slice] , 'slicedfile.dat'); // create new file
// Need to use a data transfer object to get a new FileList object
var datTran = new ClipboardEvent('').clipboardData || new DataTransfer();
datTran.items.add(newfile); // Add the file to the DT object
input.files = datTran.files; // overwrite the input file list with ours
// call function that takes path and id as parameters

node require json persisting across files

In one of my files, A.js, I call var locations = require('./locations.json'). I then modify locations.
Then in a different file I have the same thing var locations = require('./locations.json'). However instead of locations having the value of what is stored in locations.json, it now contains the changes that I made in A.js.
A.js
var locations = require('./locations.json')
function func() {
// Changing the values stored locations
}
B.js
var locations = require('./locations.json')
console.log(locations) // prints out the changes made in A.js instead of what is in locations.json
Why does this happen and is there any way around it?
Because require is for module loading, and only loads a single copy of whatever you pass it, and keeps a reference to it. See http://fredkschott.com/post/2014/06/require-and-the-module-system/ for technical details.
If you do want to simply load the json file use fs.readFileSync instead, eg:
var locations = fs.readFileSync('./locations.json');

How to get a document object from a file object in a Google-Apps Script

My problem is that I have a number of Google documents (docs) and each has a table of contents. I want to grab the TOC's and place them in another document. This should be easy, but I have run into a snag.
I can grab all of files in a folder with:
var folder = DocsList.getFolder('yourfolder');
var contents = folder.getFilesByType(DocsList.FileType.DOCUMENT);
This gives me an array of files as the variable 'contents'
Great.
I also know that
var TOC = **doc**.getAs(DocumentApp.ElementType.TABLE_OF_CONTENTS)
The problem is that I cannot figure out how to get a document object from a file object or alternately how to get an array of documents in a folder rather than an array of files.
I have searched for an answer and not only on this site. If anyone can help, I would appreciate it very much
DocsList and DocumentApp have at least one method in common which is that they have access to the ID of documents so it is quite straightforward to pass this ID parameter from one method to the other.
This code demonstrates :
function myFunction() {
var folder = DocsList.getFolder('yourfolder');
var contents = folder.getFilesByType(DocsList.FileType.DOCUMENT);
var docObject = [];
for(var c in contents){
docObject.push(DocumentApp.openById(contents[c].getId()));
}
Logger.log(docObject);// now you have an array of DocumentApp objects
}

adding element to end of array in sharedObject

I want to add an element to an array with every click button.
I used shared Object. because i'm going to call my data after i reopen my app.
how can i define an array in shared object?
i cant use this:
myData.data.list = [];
since when i open my application again, my last data will be deleted.
and how can i add elements to my variable in shared object?
i tried this:
Array(myData.data.list).push(number);
but when i trace that, every index of that is still undefined and nothing has changed.
what's the solution?
Try this steps:
First ActionScript code to create an Array, and push the element in the Array. and finally stored Array in ShraedObject. SharedObject is stored disk. so it will probably be in the form of ByteArray. so read and the data type must be converted to match.
If you want write to a Array in SharedObject
var myArray:Array = [obj1, obj2...];
var so:SharedObject = SharedObject.getLocal("myArray");
so.data.myArrayData = myArray;
so.flush();
To retrieve it back anywhere and adding element to end of array. and resaved.
var so:SharedObject = SharedObject.getLocal("myArray");
var myArray:Array = so.data.myArrayData;
myArray.push(number);
so.data.myArrayData = myArray;
so.flush();
I found something helpful that I'm going to add into the above code answer:
To retrieve it back anywhere and adding element to end of array. and resaved.
var so:SharedObject = SharedObject.getLocal("myArray","/");
var myArray:Array = so.data.myArrayData;
myArray.push(number);
so.data.myArrayData = myArray;
so.flush();
To avoid inadvertently restricting access to a shared object, use the localpath parameter. - which is the "/"

Read a file in WinRT class library

In a Windows Store app project I was reading a File like this:
var uri = new Uri("ms-appx:///DataModel/Accounts.csv");
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
var read = await FileIO.ReadTextAsync(file);
The file Accounts.csv was added with build action content.
I have now moved the code to a separate class library. File is still added with build Action content.
But the uri scheme does not seem to work anymore.
I've also tried:
var file = await StorageFile.GetFileFromPathAsync("/DataModel/Accounts.csv");
var read = await FileIO.ReadTextAsync(file);
How do I access a file in a WinRT class library? Does the file library behave differently than the Store app project?
You can access file in Windows Store class library like this.
var _Assembly = Assembly.Load(new AssemblyName("ASSEMBLY_NAME"));
var _Stream = _Assembly.GetManifestResourceStream("YOUR_FILE_NAME_WITH_PATH.xml");
Please note in class library path members are seperated by . not by \
To get all the resource names
var names = _Assembly.GetManifestResourceNames();
The GetManifestResourceStream method will always returns NULL if the resource built action property is not set to embedded resource