New to Fabrication API... Easy Question

Anonymous

New to Fabrication API... Easy Question

Anonymous
Not applicable
I'm using this: 
Item itm = Job.Items.FirstOrDefault(x => x.CID == 866);

when I want to reference the 866 straights in any giving job. However this only references the first on in the job (because of "FirstOrDefault") and doesn't touch any others in the job. I know I'm missing something here but I figured I could get the answer here quickly. 

How do I reference ALL straights in the job? 
0 Likes
Reply
Accepted solutions (1)
1,075 Views
8 Replies
Replies (8)

jazpearson
Autodesk
Autodesk
Accepted solution
var items = Job.Items.Where(x => x.CID == 866);

 which will give you an IEnumerable<Item> and if you want the items in a list, try this:

var items = Job.Items.Where(x => x.CID == 866).ToList();

 

Anonymous
Not applicable

Thank you Jaz!

 

 A few more questions if you don't mind...

 

 

ItemDimensionBase baseDim = itm.Dimensions.FirstOrDefault(x => x.Name == "Length");
ItemDimensionBase baseDim2 = itm.Dimensions.FirstOrDefault(x => x.Name == "Width");

foreach (Item value in lstStraight)
   {
     MessageBox.Show("Length: " + baseDim.Value.ToString());
     MessageBox.Show("Width: " + baseDim2.Value.ToString());
   }

I'm trying to figure out how to reference everything here and display it to make sure I am referencing everything right. 

 

I noticed that I can get the "Length" to work and display. However, I get errors when I try to add "Width" and "Depth". I've screenshotted the error. I'm clearly referencing something wrong. But I am not entirely sure what. 

 

I basically want to display a lot of information about each item so I know that I'm referencing things right. And I can proceed with my plug-ins

 

0 Likes

jazpearson
Autodesk
Autodesk

Hi, tmistretta

 

You are getting the dimension values from your itm, and then in the foreach you are looping through lstStraight. 

Then in your foreach loop, you are trying to show the message box for the values you previously created, which would only show the values from the object "itm", and not the "value" object.  

The reason for your error is that baseDim and/or baseDim2 is null. That means there is not a dimension whose name is "Length" and/or "Width" in that item's list of dimensions.

 

If you wanted to display a list of all dimension values for all your items, you could do something like this:

 

foreach (var itm in myItems) 
{
var builder = new StringBuilder();
foreach (var dimension in itm.Dimensions)
{
builder.AppendLine($"{dimension.Name}: {dimension.Value.ToString()}");
}
MessageBox.Show(builder.ToString());
}

 

Anonymous
Not applicable

Thank you! I feel I am much closer.

 

However, I need to compare the length, width, and depth to certain values that determine a custom data field. What would be the best way to pull the length, width, and depth to use for comparison? 

 

I was trying to store them in their own variables for that reason but wasn't quite there. 

0 Likes

Anonymous
Not applicable

When I declare only one "baseDim" variable, it works. When I uncomment my "baseDim2" the program freezes when ran. (See Photo)

0 Likes

jazpearson
Autodesk
Autodesk

Your baseDim variable is only checking the value of your itm object, and not each item in your lstStraight list.

 

Your baseDim2 object will be null, which is causing your programme to crash and is because itm.Dimensions does not contain a dimension with the exact name of "Width". 

 

If you want to check every "Length" dimension of the items in you lstStraight, then you should do something like:

foreach (Item value in lstStraight)
{
// this line is trying to get the Length dim for the current element in the list var baseDim = value.Dimensions.FirstOrDefault(x => x.Name == "Length");      if (baseDim == null) {
// the length dim doesn't exist in this item MessageBox.Show("Length dim does not exist"); } else {
// length dim exists - show it
MessageBox.Show(baseDim.Name + ": " + baseDim.Value.ToString()); }      }


and you can do similarly for your width dim.
You should be able to debug your object and see what dimensions on the items actually exist.....

 

0 Likes

Anonymous
Not applicable

it's indexed.....Smiley Frustrated.....

 

I should have already known that because that's how the camduct scripting works as well. 

 

hehe I feel like an idiot now.....

 

one last question and i will stop bothering you.

 

How do I properly debug objects so this doesn't happen to me again?

0 Likes

jazpearson
Autodesk
Autodesk

Are you using Visual Studio?

 

If so, you can Attach to Process in the Debug menu whilst your application is running. Make sure to pick whatever application you are running, e.g. ESTmep or CAMduct, and also make sure to select managed (4.6...) code type.

 

Then you can stick breakpoints in your code and you should be able to inspect your objects - although may need to make sure you are running in debug mode. 

 

I'd reccommend looking at the following article for any help:

https://docs.microsoft.com/en-us/visualstudio/debugger/getting-started-with-the-debugger

 

 

0 Likes