您的位置:首页 > 其它

My First Plug-in - Inventor API .NET 开发从0开始-课程1

2012-10-31 18:02 681 查看
首先需要安装 Autodesk®
Inventor® 和 Microsoft® Visual Basic® Express。这个系列课程基于VB.NET讲解,但同时也提供了C#源码工程。所以,若用C#,需要安装Visual
C# Express。安装后,启动Inventor和Visual Basic,看看有无什么问题。

课程1演示如何建立独立的EXE工程,然后连接到Inventor,通过API对选择对象实现隐藏的操作。原文地址:

Lesson 1: The Basic Plug-in

我们来看看步骤。 先下载提供的VB.NET和C#代码工程,解压之。



lesson1_vb-net.zip (zip
- 49Kb)


lesson1_c-sharp.zip (zip
- 73Kb)
Steps to Create Your First Plug-in

Launch the Visual Basic Express development environment:

Open Visual Basic 2010 Express using the Windows Start menu, selecting All Programs, then Microsoft Visual Studio 2010 Express, and
then Microsoft Visual Basic 2010 Express. Note: You can also use Visual Basic 2008 Express with this guide. Projects for both 2010 and 2008 are provided.

启动Visual Basic。

Open a class library project:

Inside Visual Basic Express, on the File menu, click Open
Project. Navigate to the subfolder of the supporting material you downloaded at the top of this guide called lesson1_VisualExpress2010 and
open the VB.NET project contained within it by selecting the project file MyFirstInventorPlugin_Lesson1.vbproj.

点击【文件】>>【打开项目】>>选择到刚才下载的文件所在路径,找到这个文件:MyFirstInventorPlugin_Lesson1.vbproj,打开它。

Open the code:

In the open project you will see a form with one button (if you don’t see the Form, click on Form1.vb in theSolution Explorer frame in the upper right hand side). Right
click on Form1 in the Solution Explorer and select View Code or just double click on the Form.

你将在Visual Basic里看到如下状态。在右面的浏览窗口,可以看到一个名为Form1.vb的文件,右键点击【查看代码】,或双击它。

本项目已经为大家创建了一个Form(窗体)项目。其中有个button(按钮)。



Add the code:

In the code window, type the code below into the Sub Button1_Click. (This is what runs when the button is clicked.) You may need to scroll down towards the bottom of the code to find the place to add the below code, looking for the words ‘Add
code for Lesson 1 here’. To get the full experience of developing with Visual Basic Express – including the use of features such as IntelliSense – we recommend you type the code from this guide rather than copying and pasting it. That said, if constrained
for time you can also copy and paste into the Visual Basic Express code window, although this will reduce the experience you gain from working with the code directly.

打开的代码页面,可以发现名为 Sub Button1_Click 的函数。我们演示用的代码将放在这里。以下就是需要贴进去的代码。复制粘贴即可。

If _invApp.Documents.Count = 0 Then
  MsgBox("Need to open an Assembly document")
  Return
End If
 
If _invApp.ActiveDocument.DocumentType <> _
DocumentTypeEnum.kAssemblyDocumentObject Then
  MsgBox("Need to have an Assembly document active")
  Return
End If
 
Dim asmDoc As AssemblyDocument
asmDoc = _invApp.ActiveDocument
 
If asmDoc.SelectSet.Count = 0 Then
  MsgBox("Need to select a Part or Sub Assembly")
  Return
End If
 
Dim selSet As SelectSet
selSet = asmDoc.SelectSet
 
Try
  Dim compOcc As ComponentOccurrence
  Dim obj As Object
  For Each obj In selSet
    compOcc = obj
    Debug.Print(compOcc.Name)
    compOcc.Visible = False
  Next
Catch ex As Exception
  MsgBox("Is the selected item a Component?")
  MsgBox(ex.ToString())
  Return
End Try


Save the file:

On the File menu, click Save All.

保存文件,点击Visual Basic【文件】>>【保存】

Build the project:

The code you have written is in human readable form. To make the code readable by a computer, you will need to translate it or “build” it.

Inside Visual Basic Express, in the Debug menu,
click Build
Solution to compile and build
your plug-in. The “Build
Success” message shows in status
bar of the Visual Basic Express window if the code is successfully built.

现在编译这个工程,点击【调试】>>【编译
MyFirstInventorPlugin_Lesson1】。工程就可以开始演示了!很快吧!



That’s it! You have just written your first plug-in for Autodesk Inventor. Let’s run the plug-in to see what it does.

下面是看如何运行

Running the Plug-in

Start Autodesk Inventor. (Note: When the plug-in is run it will start a new session of Inventor if one is not already open.)

手动启动Inventor。注意代码是这样实现的:

-如果已经有Inventor在运行,就用这个

-如果没有,则会启动一个

一般我们喜欢手动启动好,而不用代码去做。这样就不用每次启动Inventor了。

Create or open an existing Inventor assembly:

Either unzip the file Clutch_Bell_Simplified.zip,
and open the Clutch_Bell_Simplified.iam assembly or within Inventor make sure you have an assembly of your choosing active. There are several types of document that can
be created or worked with inside Inventor. The most commonly used document types are Part (.ipt), Assembly (.iam) and Drawing (.idw). Open a new assembly and place some parts using the standard Inventor user-interface.

手动创建一个装配,有些零件。或者直接下载样例文件 Clutch_Bell_Simplified.zip



Run your plug-in with Inventor and allow the plug-in to communicate with Inventor:

To make Visual Basic Express execute the code you have entered, select Start Debugging on the Debugmenu (you can use the F5 key or click on the green arrow – which looks
like a “play” button – on the Debugging toolbar). This will cause your form to be displayed. You may need to minimize VB Express to see both the form and Inventor.

回到Visual Basic,点击【调试】>>【开始调试】。你的程序就启动了,将会看到含有按钮的那个窗体弹出。




Work with the plug-in:

Select one or more (by using the Ctrl key) components in the assembly that is active inside Inventor and then click Button1 on the form to execute your code and hide the selected components.

在Inventor里,手动拾取某个部件,例如下图中的Clutch Gear 20t:1。点击程序的 Button1。将会看到这个部件隐藏了。



To re-display the invisible components use the Inventor Assembly browser (you can identify them via their component icons, which should now be grayed out). In the browser, right-click on the invisible components and pick Visibility,
making them visible once again.

还可以手动的修改可见状态(右键部件,选择【可见性】),再重新执行一遍程序。



Congratulations! You have just written your first plug-in for Autodesk Inventor. You will be reviewing the code in detail in Lesson 3.

Before you move on to the next lessons, let us go back to some of the things we skipped over earlier, starting with basic concepts about programming, and the benefits it can bring to your day-to-day work.

其实过程并不复杂。相信你现在已经对下个课程跃跃欲试了 。目前你可能还不知刚才贴的代码到底是怎么意思,别着急,接着看后面的课程。

或许你有一些小疑问,希望下面这些部分能有所帮助。

Additional Topics


Introduction to Programming

The VB.NET code you have just executed that hides the selected components is only 30 lines long and more than half of the code that you entered into the project is doing error checking. The code that actually does the work can be narrowed down to these few
lines of code:
Dim asmDoc As AssemblyDocument
asmDoc = _invApp.ActiveDocument

Dim selSet As SelectSet
selSet = asmDoc.SelectSet

Dim compOcc As ComponentOccurrence
Dim obj As Object
For Each obj In selSet
  compOcc = obj
  compOcc.Visible = False
Next


As you can see, a small amount of code can go a long way to simplify working with Inventor. Software programming allows you to capture the logic of a particular manual procedure once and then reap the benefits over and over again, every time you want to perform
this functionality.

首先,前面短短30行代码就达到了我们的目的。而且可以反复使用,这就是API的力量。


What is Programming?

A simple answer to this question is: Computer programming is the process of creating a sequence of instructions to tell the computer to do something. You can look at your program as a sequence of instructions. During the course of the upcoming lessons, you
will look at the various lines and blocks of code in the context of being instructions for a computer.

If you were to explain what computers are to a young child, you might say: a computer is a tool that follows instructions you provide. Programming is one way of giving instructions to the computer. Internally, a computer sees these instructions encoded as a
series of numbers (also called machine code). The human-readable instructions you saw at the beginning of this lesson are called source code and the computer converts these instructions into machine code which it can then read and execute. A sequence of such
instructions (or code), written to perform a specific task, is called a program and a collection of such programs and related data is called software. Autodesk Inventor is one such software product.

Source code can be written in different languages, just as humans use different languages to communicate between ourselves. The language you will be using in this guide is called Visual Basic.NET (VB.NET).

这是在说什么是编程。好大的话题!我们今天所在的世界,方方面面面,都有编程在其中,就是这些程序为我们的生活工作带来了巨大变化。而Inventor API,则为从事Inventor设计的人员提供了有力的工具。


What is an API?

API is the acronym for Application Programming Interface: the way a software programmer can communicate with a software product. For instance, the Inventor API is the way programmers can work with Inventor, and establishes what functionality a software programmer
can use within Inventor. Such as the Inventor API allows you to write instructions for Inventor to execute one after the other.

这里在说API是什么。总体讲,就是连接二次开发程序和Inventor之间的桥梁。通过它,你可以操作Inventor文件,模型,达到自己的需求。拓展产品本身的能力。



Putting this slightly differently: commercial software companies, such as Autodesk, often distribute a set of libraries that you can use in your own program to interact with a particular software product, such as
Autodesk Inventor, and extend its functionality. This set of libraries is known as the software product’s API.

The type of program you write to interact with a software product and extend its functionality will depend upon how the API has been designed and what has been exposed (through APIs) for you to work with.


What is a Plug-in?

A software plug-in is a type of program module (or file) that adds functionality to a software product, usually in the form of a command automating a task or some customization of the product’s behavior. When you talk about a plug-in for Inventor – and you
will also hear the term AddIn or Application used for this product – we mean a module containing code that makes use of the Inventor API. The code can connect to Inventor to automate tasks, or be loaded by Inventor and used to adjust its behavior of Inventor
under certain conditions, such as when a particular command is executed by the user of the plug-in.

For terminology purposes, an Inventor AddIn would also be considered a plug-in. An AddIn is a special kind of plug-in that automatically loads when Inventor is started, has high performance and appears to the user to be part of Inventor..

这里解释了什么是Plug-in。我们也有时叫做Add-In。中文就是插件。顾名思义,插件是嵌入到Inventor里的。课程1实质上是一个独立的EXE程序。插件和EXE最大区别在于前者运行在Inventor进程里,而后者是独立的进程。所以一般为了获取最大的效率,插件是最好的选择。在本课程里,为了对应课程名“My First Plugin”,把这种独立的EXE也称为“Plug-in”,我们可以理解为广义上使用Inventor
API的程序。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: