Function not Working as Anticipated

Why does one work and not the other?

You’re not returing anything from your function, hence null.

Try adding return = Sheet.ByNameNumber....

So without the return it doesn’t actually do anything? thanks

Anything inside your block of code will execute. Without a return statement the process still executes but is effectively redundant as objects are instantiated while the code is executing then discarded once the process completes.

In this case then, did it make the sheet and then discard it? Or maybe it was ready to make the sheet but without the return didn’t put it through to Revit?

Yes, anything in your block of code which runs without error will start a new process on your processor and placed data on your computers memory. Once this process completes, its dumped from memory (garbage collection). The return statement enables the programmer to specify what should be returned (what should be stored on memory) as you will normally be instantiating multiple objects to get to your end-goal, but in most cases, only need a few of those objects to be output.

1 Like

Would a Null be considered an error in this case? The Sheet.ByNameNumberTitleBlockAndViews, if fed more views than fit on a sheet, will create the sheet but return Null.

No, null is an object. Its empty or void. Object-orientated languages cant return ‘nothing’ so null is the object that used to mean the same thing.

This helped a lot. Thanks.

Last question (I hope); can we suppress the return showing as an output of the DS node? My current script creates a bunch of sheets but doesn’t list any of them (as I suspected it wouldn’t because I’m sending the Sheet.node more views than it can handle. To get the script to complete I had to call the sheet creation node one more time outside of a while-loop with enough views that, instead of a null, I get a sheet output. That’s fine but now it looks confusing because the output is a single sheet and it made 12 (for example).

If I can have it return, but not show that it returned in the node output that would be one solution.

If its in a function and written correctly then there will be no visible output from the DS node:

The rest of what you are saying is difficult to follow and it sounds like there’s a problem with the way you have structured your code. If you are not declaring an imperative block then you are using associative DS (what is shown in my example). From what I can glean from what you have said, the problem may be caused by the recursion process failing as it sounds like you have list inputs that aren’t equal in length.

Can you elaborate and show a screenshot or something.

@Thomas_Mahon – I am declaring an Imperative block as I understood it was required for loops. I don’t really know what an Imperative is so that could be hugely inaccurate. The code currently looks like this (annotated for the discussion);

def ViewsToSheets(sheetName:var[],sheetNumber:var[],
titleBlockFamilyType:var[],views:var[])
//upon further research, I think I can do without the var[] or, better perhaps, declare them as string, string, familytype, and view, respectively.
{
i=0;
viewsCount=0;
numberViews = List.Count(views);
count = List.Count(views);
return = [Imperative]
{
	while (count>0)
	{
		MakeSheet(sheetName,sheetNumber,titleBlockFamilyType,views);
        //a separate def to call the Sheet creation command - thought i needed to allow for next step
		viewsCount=CheckSheet(views,sheetNumber);
        //a separate def to determine how many views where just placed (sheet node returns null when given a list larger than a sheet will hold - the separate defs seemed to be necessary at the time - figured it was like a transaction?
		views=List.DropItems(views,viewsCount);
		sheetNumber=NextSheetNumber(sheetNumber);
        //last def to increment an alphanumeric sheet number by 1
		count=count-viewsCount;
		i=i+1;
	}
	sheet = MakeSheet(sheetName,sheetNumber,titleBlockFamilyType,views);
    //the while loop doesn't place all views - seems to stop short so I added this to place the last few views
	return = {sheet,(numberViews) + " views placed on "+(i+1)+" sheets."};
    //guessing based on your last comment that I could return the MakeSheet line instead to not have the node output anything but I also thought it nice to have the number of views placed on sheets notification
}    
};

Another thread suggested the count>0 should be count>=0 but that either runs forever (or until I throw in the towel, or give me 6-8,000 loops instead of the 3 it should in my test file.

while (i<=count):

Current solution will run forever, cause while loop executes till it’s true.
So if you feed
1 view -> count(1view) = 1
while (1 >0) it’s always true so is can’t stop

If I were you I would use a for loop. Use while loops only where necessary (typically where you don’t know the number of cycles needed to reach a desired output - eg. optimisation, unpredictable rank reduction etc). There seems to be a habit of using while loops instead of for loops which isn’t a good thing since they’re more complicated to understand and lead to the very problems you’re encountering!

If you do want to stick with while loops then try what @Tomasz_Puchala has suggested.

Also the problem that seems to be affecting your code is the same as before pretty much: you create sheets as the while executes but don’t store the result. When the while loop completes, all of those sheets will be discarded and the only one you’ll get out of the function is the one you assigned to your sheet variable.

Try creating a list to store the sheets on each cycle then return it. You can then get rid of the last MakeSheet call.

I won’t know how many times to loop, hence the while loop.

Not sure I’ve been clear on the issue. I have x views and I want them on y sheets. I don’t know how many views will fit on each sheet. If I give Sheet.ByNameNumberTitleBlockAndViews more views than fit on a sheet it returns null (so there’s no sheet output from the call to store) and stops but still makes a sheet filled with views. So, I figured if I make the sheet and count the views it placed (z) I can shorten my view list by x-z and run another Sheet.By call. This part loops. My whole-loop iterates on a reduced x but with a variable z instead of a constant 1. When x is zero it should stop but if I look for that it runs forever.

If I run the logic step by step it looks like it should work at count>0 but it doesn’t. Example;

  • 13 views, place 4, 9 greater than 0, loop
  • 9 views, place 3, 6 greater than 0, loop
  • 6 views, place 5, 1 greater than 0, loop
  • 1 view, place 1, 0 not greater than 0, stop
    This looks like it runs 4 times but I only get 3 sheets created and I still have 1 view left, hence the final sheet call.

What I have works so I’m inclined to leave it for now. Having the DS show the single sheet instead of nothing or a list of sheets isn’t the end of the world. I’ll add a note to explain and find something else to do.

Thanks again for your help. I think I understand things a bit better now.