Adventures in the Autodesk Inventor API

Wednesday
Home / Autodesk Inventor API Adventures – Wednesday

(Sometimes as a developer you get to be a super hero and overcome incredible odds. Of course, it helps if you have the skills to pull off magic – in this week-long story we paint a tongue-in-cheek picture of one of those situations. Check out the beginning of the week posts first Monday, Tuesday, and continue below)

Riding up the full elevator, you start humming to the theme of Rocky. The others in this confined space give you the “what is wrong with this guy” look, but you brush it off because you know today is a good day and nothing can bring you down. The doors open and you walk out, grab the morning doughnut and proceed to your designated station. As you ease into your luxurious high-back chair, you realize that your yellow-stained 17” CRT monitor has been replaced with dual 32” flat screens. You are hypnotized by their gleam and without looking, reach down to turn on your computer. You fumble around hunting for the power button that you have pressed over a thousand times in the last 5 years. You summon the courage and break the spell cast upon you from said monitors and glance down to notice the brand new computer below. You pause for a moment to say a small obituary for the old one and hastily power it up. It is all you ever expected. And look, all your files are there, just like your old one (we all know that is a lie, but go with it). Let’s test this thing out!

Free User-Friendly Software Checklist

The_user-friendly_software_checklist_-_provided_by_unstoppable_software_pdf__page_1_of_4_

In the last ten years, we've created hundreds of SUPER user-friendly applications that our customers LOVE to use. Our secret? We follow these 7 simple guidelines. Sign up to receive your FREE copy of our checklist now.

Powered by ConvertKit

Loading up the solution in mere seconds only makes you think “man, at this rate, I’ll be out of here before lunch”. John from the next cubical blurts “You leaving at lunch?” Just then, you realize you weren’t just thinking that. You blurt something incoherent back at him and gawk on the vast real estate before you and focus on what you need to do for today… Load the IDW and get the scaling working.

For today, we will need to modify the main dialog to allow for the loading of the IDW and to initiate scaling. Here is what it looks like now.

WednesdayDialog

Then, let’s get that new IDW loaded and the view brought in. This is a very simple operation, so you decide to set a milestone, that when you are done with this, you will play a game of solitaire that is expanded across both screens, just because you can. So, without hesitation, you start typing the new code.

private Inventor.DrawingDocument m_idw = null;

Followed by

private void btnLoadIDW_Click(object sender, EventArgs e)
{
	m_idw = m_application.Documents.Open(@"c:\blog.idw") as DrawingDocument;
}

You fire it back up and presto!!! You got it on the first try.

IDWload

Your next mission is to get that view loaded from the assembly. This takes a bit more work, but you are not worried one bit. You grab a piece of licorice and use it as a straw to take a sip of coffee. You decide that that was a very bad idea and just chomp away at the licorice and drink the coffee separately. As you finish up the final bite, you again focus on the task at hand. Creating the button hooks and manipulating Inventor. You suddenly realize you will need projected as well as the base view and from your knowledge of Inventor, you know that you must create both of those views before any scaling can be done to them.

**I have broken them up into separate methods / events, but I would not do that in my final product**

private Inventor.DrawingView m_baseView = null;
private Inventor.DrawingView m_projectedView = null;
private void btnCreateBaseView_Click(object sender, EventArgs e)
{
	Sheet sheet = m_idw.ActiveSheet;
	Inventor.Point2d position = m_application.TransientGeometry.CreatePoint2d(0, 0);
	m_baseView = sheet.DrawingViews.AddBaseView((_Document)m_mainAssembly, position, 1.0, ViewOrientationTypeEnum.kIsoBottomRightViewOrientation, DrawingViewStyleEnum.kHiddenLineDrawingViewStyle);
}
private void btnProjectedView_Click(object sender, EventArgs e)
{
	Sheet sheet = m_idw.ActiveSheet;
	Inventor.Point2d position = m_application.TransientGeometry.CreatePoint2d(0, 0);
	m_projectedView = sheet.DrawingViews.AddProjectedView(m_baseView, position, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle, 1);
}

The results after each click quickly appear on this mammoth of a new machine.

Base View Creation:

BaseView

Projected View Creation:

ProjectedView

I have inserted them at the origin (0,0) so that I can position them later via the API, but you can insert them in the correct location in the initial call.

Alright, let’s get down to scaling. We have determined that 80% of ½ of the sheet is the size required. You calculate the height factor and the width factor to resolve this small scaling dilemma. Executing the code, you notice that the projected view is linked to the base view, so only one scaling is required.

private void btnScaleBaseView_Click(object sender, EventArgs e)
{
	// scaling it to 1/2 the width minus 20%
	// must determine which is bigger, the width or height direction
	Sheet sheet = m_idw.ActiveSheet;
	double widthFactor = (sheet.Width / 2) / m_baseView.Width;
	double heightFactor = (sheet.Height / 2) / m_baseView.Height;
	double factor = Math.Min(widthFactor, heightFactor) * .8;

	m_baseView.Scale = factor;
	m_idw.Update();
}

After the scale, you should have the following on screen, which is exactly what you expected.

ScaledViews

You just realize you didn’t get your game of solitaire in, but who cares, you got one step left and you can call it a day. You dive back into the code and position those views where they need to be.

private void btnPositionBaseView_Click(object sender, EventArgs e)
{
	Sheet sheet = m_idw.ActiveSheet;
	// position on sheet for center of view
	double x = sheet.Width / 4;
	double y = sheet.Height / 2;

	Inventor.Point2d position = m_application.TransientGeometry.CreatePoint2d(x, y);
	m_baseView.Position = position;
	m_idw.Update();
}

private void btnPositionProjectedView_Click(object sender, EventArgs e)
{
	Sheet sheet = m_idw.ActiveSheet;
	// position on sheet for center of view
	double x = 3 * (sheet.Width / 4);
	double y = sheet.Height / 2;

	Inventor.Point2d position = m_application.TransientGeometry.CreatePoint2d(x, y);
	m_projectedView.Position = position;
	m_idw.Update();
}

You load up the main dialog again and push the buttons, you what you see projected on the new monitors is just amazing.

PositionBaseView

PositionProjectedView

Your day is complete. As you come out of your programming “zone”, you hear your boss quickly approaching. You do not want to spend the next hour in a conversation with him, so you quietly turn off your computer and crouch as you move down the row of cubicles to the elevator and slip into the sanctuary of the steel box as you hear Sammy Hagar’s “I can’t Drive 55”. What a day!

Recommended Posts

Leave a Comment

Start typing and press Enter to search