Javascript and bookmark names

Javascript and bookmark names

Matt__W
Advisor Advisor
2,044 Views
16 Replies
Message 1 of 17

Javascript and bookmark names

Matt__W
Advisor
Advisor

Got another one for you @elliott.rosenfeld.  🙂

 

I have a model with three bookmarks in it. When I run the code below.....

var doc = app.ActiveDocument();

var varBM = doc.Bookmarks;
for (x in varBM) {
   print (varBM[x]);
}

 

...it returns this at the debug window.

adsk::js::Bookmark(name = "")
adsk::js::Bookmark(name = "")
adsk::js::Bookmark(name = "")
Script evaluation: 0.002 seconds

Which says to me it's not recognizing the names of the bookmarks and returning an empty value? And what's the deal with "adsk::js::"? Is IW / JS just being polite with its error messages?!?

 

 

*heads off to scour the IW JS documentation and inner-webs for clues*

 



Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Accepted solutions (1)
2,045 Views
16 Replies
Replies (16)
Message 2 of 17

elliott.rosenfeld
Autodesk
Autodesk

Hey Matt, 

 

 

Haven't forgotten about you. I've been trying to puzzle this one out, and I am thinking that the issue here may be because the Bookmark metadata is stored in JSON inside a model's unver folder. Just for reference, here's what I am thinking may work:

 

Step  1) 

// Set a variable for your Bookmarks folder, defining where the bookmarks are stored for your model

var bmFolder = "C:/Users/yourusername/Documents/Autodesk InfraWorks Models/Autodesk 360/modelid#/modelname.files/unver/Bookmarks";

 

Step 2)

//Retrieve the active model and document

var model = app.ActiveModel;

var doc = app.ActiveDocument();

 

Step 3)

//Process each file in bmFolder (still not 100% certain the syntax for this)..

 

Step 4)

//parse the JSON strings within your bmFolder

 

 

Step 5) 

//print

 

Thanks,

Elliott


Principal Specialist, Infrastructure
0 Likes
Message 3 of 17

Matt__W
Advisor
Advisor
So I wasn't losing my mind thinking that the Bookmarks function isn't fully functional? Glad to see it wasn't something I was doing wrong.

I'll hack away at this tomorrow and see what I can scratch together.


Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 4 of 17

Matt__W
Advisor
Advisor

So I've got this together but I'm getting stuck on iterating through the Bookmarks folder to search for JSON files. I can't seem to tap into the Script.FileSystemObject (which would make this infinitely easier) so now I'm at a loss. Nothing I've tried has worked so far. Below is my code. Any thoughts? Can anyone at the factory put me in the right direction?? This is my first real attempt at Javascript so I'm on a mission to figure this out and learn from it. You could say it's gotten personal now.  🙂

 

// Some global vars...
var curModel = app.ActiveModel;
var db = curModel.Db;
var fPath = db.FilesPath;
var bMarks = fPath + "unver/Bookmarks"
print (bMarks);

if(!file.FolderExists(bMarks)) {
      alert("Folder doesn't exist");
   }
   else {
//      alert("Folder exists"); // The folder still exists even if all bookmarks have been deleted
      print ("");
      print ("========================================");
      print ("Loop through directory and get all files");
      print ("========================================");
      print ("");

// Code that I found online that I attempted to make work but... var fileList = getFiles(bMarks); <<--- THIS IS WHERE IT DIES. for (var i = 0; i < fileList.length; i++) { if (fileList[i] instanceof File) { print (fileList[i]); } } }


Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 5 of 17

elliott.rosenfeld
Autodesk
Autodesk
Accepted solution

Hey Matt,

 

So a colleague helped me out with this one.

Looks like your original line of thinking was actually pretty close. Try this out, it seems to work pretty nicely:

 

var bmList = app.ActiveDocument().Bookmarks;
for(var i=0; i<bmList.length; i++) {
    var bm = bmList[i];
    print((i+1) + ": '" + bm.name + "'");
}

Best,

Elliott


Principal Specialist, Infrastructure
Message 6 of 17

Matt__W
Advisor
Advisor

This is perfect! Thank you so much!


{bro hug}

 

or fist bump... whatever.   🙂



Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 7 of 17

elliott.rosenfeld
Autodesk
Autodesk

Hah great, back atcha. Although I can't take full credit, had to use my lifeline to answer this one.


Principal Specialist, Infrastructure
0 Likes
Message 8 of 17

Matt__W
Advisor
Advisor

One more hopefully quick question....

When I use the following code, Infraworks just blows through it and only sets the last bookmark as current. It's as if Infraworks isn't updating the view.

 

I've tried the Regenerate function, ProcessEvents, Update and UpdateFeature (both of which I don't think apply since I'm not writing anything to a database).

 

How do I get it to stop and open each bookmark and set it current??

 

var doc = app.ActiveDocument();

var bmList = app.ActiveDocument().Bookmarks;
for(var i=0; i<bmList.length; i++) {
    var bm = bmList[i];
    print((i+1) + ": '" + bm.name + "'");
    doc.MoveToBookmark(bm.name);
}


Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 9 of 17

Matt__W
Advisor
Advisor

Sorry for the barrage of questions but I'm a newb when it comes to JS coding and I REALLY want to learn this and figure it out (with some help when needed).



Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 10 of 17

Matt__W
Advisor
Advisor

Okay @elliott.rosenfeld.... So I found that if I use a WAIT function, I can get IW to actually show the bookmarks. But I don't have that warm and fuzzy feeling that that's the "correct" way to go about it. Comments? Suggestions? Care to use another life line?  🙂

 

var doc = app.ActiveDocument();

function wait(ms){
   var start = new Date().getTime();
   var end = start;
   while(end < start + ms) {
     end = new Date().getTime();
  }
}

var bmList = app.ActiveDocument().Bookmarks;
for(var i=0; i<bmList.length; i++) {
    var bm = bmList[i];
    print((i+1) + ": '" + bm.name + "'");
    doc.MoveToBookmark(bm.name);
    wait(1000); // wait 1 second before continuing
}


Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 11 of 17

elliott.rosenfeld
Autodesk
Autodesk

Hey Matt,

 

Sorry for the late response, i've been at a conference. The WAIT function does seem to function (pun intended), but you are right that there may be a better way. Nice work getting that to work, and sure I'll ask some colleagues about any possible improvements!

 

Best,

Elliott


Principal Specialist, Infrastructure
Message 12 of 17

Matt__W
Advisor
Advisor
Thanks. I appreciate your help (and patience) on this. 🙂


Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 13 of 17

Matt__W
Advisor
Advisor

@elliott.rosenfeld any progress on this?  I feel like I've been WAITing forever! (wakka wakka wakka) I have to believe there's a more correct way to do this because if the wait period isn't long enough, Infraworks could still be processing while the JS continues to run which would mess things up. Processor speed and model size both play a factor in this so I hope there's a function that will handle this, otherwise I may need to bump up the wait time to allow Infraworks to catch up, so to speak.



Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 14 of 17

Matt__W
Advisor
Advisor

*BUMP!*

 

@elliott.rosenfeld, any chance you've had the opportunity to discuss the wait issue with anyone?? (I just got back from a 2-week vacation which is why I didn't bother you about this earlier. Smiley Wink)



Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 15 of 17

elliott.rosenfeld
Autodesk
Autodesk

Hey Matt, 

 

Great timing, I just heard back from some colleagues today.

 

(and sorry for the delay! We had a company week of rest, then I actually had some extra time away from the forum to move, as I have just switched office locations.)

 

Here's what I was told after I forwarded this thread and your question to some colleagues: "While we are not aware of a function that asks AIW if it is ready, you may want to try to replace the WAIT function with app.Sleep(1) instead."

 

Let me know how this works out for you! If that doesn't provide the solution you're looking for, we can continue to dive into this and find a better way.

 

Best,

Elliott


Principal Specialist, Infrastructure
0 Likes
Message 16 of 17

Matt__W
Advisor
Advisor

So no built in way of determining if a command has finished? Such as setting a bookmark current... *shrugs* Okay. I'll go with the sleep function since it's a built-in function and not something home-grown.

 

Thanks for all of your help, @elliott.rosenfeld!!



Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes
Message 17 of 17

Matt__W
Advisor
Advisor

Soooo... after a little more in-depth testing with a few other models it appears I *don't* need a sleep or wait function after all to achieve what I'm doing. Smiley Mad

 

Stay tuned for an AKN article about batch printing snapshots using JS.  Smiley Happy

Thanks again for putting up with me and my questions @elliott.rosenfeld.



Matt Wunch
Revit Subject Matter Expert/sUAS Pilot

Twitter | LinkedIn

AU2017 - Code Blue Dr Revit - How to Resuscitate Corrupt Revit Models

Was this answer helpful? If so, please click the ACCEPT AS SOLUTION or the KUDO button.

0 Likes