Inventor二次开发入门.doc

上传人:苏美尔 文档编号:5685380 上传时间:2020-07-22 格式:DOC 页数:74 大小:1.89MB
返回 下载 相关 举报
Inventor二次开发入门.doc_第1页
第1页 / 共74页
Inventor二次开发入门.doc_第2页
第2页 / 共74页
Inventor二次开发入门.doc_第3页
第3页 / 共74页
Inventor二次开发入门.doc_第4页
第4页 / 共74页
Inventor二次开发入门.doc_第5页
第5页 / 共74页
点击查看更多>>
资源描述

《Inventor二次开发入门.doc》由会员分享,可在线阅读,更多相关《Inventor二次开发入门.doc(74页珍藏版)》请在三一文库上搜索。

1、Inventor二次开发入门Inventor二次开发入门1课程1:开始接触InventorAPI,用VB.NET写一个最简单的插件,实现选择集的隐藏2课程2:帮助了解VisualStudio编程环境以及基本需要熟悉的方面12课程3:深入解释课程1里出现的InventorAPI相关代码,帮助了解相关对象25课程4:学习简单的带界面程序,了解其中的逻辑,以及如何操作选择集35课程5:操作对象的附着属性(Attributes)45课程6:基于前面课程,制作一个更加丰富的插件61课程7:深入学习的建议70课程1:开始接触InventorAPI,用VB.NET写一个最简单的插件,实现选择集的隐藏在本课中

2、,你将开始写一个使用Autodesk Inventor API基于VB.NET隐藏所选组件的应用程序。演示视频(英文)演示代码lesson1_vb-net.zip (zip - 49Kb)lesson1_c-sharp.zip (zip - 73Kb)根据步骤来创建你的第一个插件1. Launch the Visual Basic Express development environment: Open Visual Basic 2010 Express using the Windows Start menu, selecting All Programs, then Microsoft

3、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.2. Open a class library project: Inside Visual Basic Express, on the File menu, click Open Project. Navigate to the

4、 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.3. Open the code: In the open project you will see a form with one button

5、(if you dont see the Form, click on Form1.vb in the Solution 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. 4. Add the code: In the code window, type the code below into the Sub Button1_Click. (This i

6、s 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 Inte

7、lliSense 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.If _invApp.Docum

8、ents.Count = 0 ThenMsgBox(Need to open an Assembly document) ReturnEnd IfIf _invApp.ActiveDocument.DocumentType _DocumentTypeEnum.kAssemblyDocumentObject Then MsgBox(Need to have an Assembly document active) ReturnEnd IfDim asmDoc As AssemblyDocumentasmDoc = _invApp.ActiveDocumentIf asmDoc.SelectSet

9、.Count = 0 Then MsgBox(Need to select a Part or Sub Assembly) ReturnEnd IfDim selSet As SelectSetselSet = asmDoc.SelectSetTry Dim compOcc As ComponentOccurrence Dim obj As Object For Each obj In selSet compOcc = obj Debug.Print(compOcc.Name) compOcc.Visible = False NextCatch ex As Exception MsgBox(I

10、s the selected item a Component?) MsgBox(ex.ToString() ReturnEnd Try5. Save the file: On the File menu, click Save All. 6. 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

11、 Express, in theDebugmenu, clickBuild Solutionto 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. Thats it! You have just written your first plug-in for Autodesk Inventor. Lets run the plug-in to see

12、what it does.Running the Plug-in1. Start Autodesk Inventor. (Note: When the plug-in is run it will start a new session of Inventor if one is not already open.) 2. Create or open an existing Inventor assembly:Either unzip the file Clutch_Bell_Simplified.zip, and open the Clutch_Bell_Simplified.iam as

13、sembly 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 part

14、s using the standard Inventor user-interface.3. 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 Debug menu (you can use the F5 key or click on the green arrow which looks

15、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.4. 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 cli

16、ck Button1 on the form to execute your code and hide the selected components. 5. 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 pic

17、k 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 basi

18、c concepts about programming, and the benefits it can bring to your day-to-day work.Additional TopicsIntroduction to ProgrammingThe 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 er

19、ror checking. The code that actually does the work can be narrowed down to these few lines of code:Dim asmDoc As AssemblyDocumentasmDoc = _invApp.ActiveDocumentDim selSet As SelectSetselSet = asmDoc.SelectSetDim compOcc As ComponentOccurrenceDim obj As ObjectFor Each obj In selSet compOcc = obj comp

20、Occ.Visible = FalseNextAs 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 functional

21、ity.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 va

22、rious 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 compu

23、ter 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

24、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 communica

25、te between ourselves. The language you will be using in this guide is called Visual Basic.NET (VB.NET).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

26、 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.Putting this slightly differently: commercial software companies, such as Autodesk, often d

27、istribute 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 products API. The type of program you write to interact with a software product an

28、d 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 automatin

29、g a task or some customization of the products 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 b

30、e 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 automaticall

31、y loads when Inventor is started, has high performance and appears to the user to be part of Inventor.课程2:帮助了解VisualStudio编程环境以及基本需要熟悉的方面In the previous lesson, you saw how you can increase productivity in Autodesk Inventor by implementing a plug-in built from a small amount of VB.NET code.You will

32、probably have heard the terms COM and .NET from Lesson 1 with reference to programming with Inventor. Both COM and .NET are technologies that enable communication between software: if you are interested in learning more, you will find information in the Additional Topics section here.You will now lo

33、ok more closely at what happened when you executed the code in the previous lesson.代码示例lesson2_vb-net.zip (zip - 19Kb)lesson2_c-sharp.zip (zip - 23Kb)What does it mean to “build” code?The code that you typed in to Visual Basic Express in Lesson 1 was a set of human-readable instructions (source code

34、) that needed to be converted into code that could be understood and executed by the computer. The “build” you performed did just that: it packaged up the resulting executable code inside a standard Windows EXE file. Its also possible to create a DLL (Dynamic-Link Library) that can be loaded into Au

35、todesk Inventor, but thats a more advanced topic not covered by this guide. The following screenshot shows the output EXE along with the associated program debug database (which provides additional information when troubleshooting the EXE), once you have built the solution in Lesson 1 using Visual B

36、asic Express. The path to which the EXE gets compiled is specified in the Visual Basic Express project settings and is set, by default, to the bin sub-folder of the Visual Basic Express project folder. Choosing a Programming Language and Development ToolJust as humans use different languages to comm

37、unicate, you have various language options available to you when creating an Inventor plug-in: for the purposes of this guide we have chosen Visual Basic .NET (VB.NET), a strong general-purpose programming language that is popular with Inventor developers.There are a number of tools available for de

38、veloping VB.NET code. They range from open source tools such as SharpDevelop to Microsofts flagship, professional development environment, Visual Studio. In your case you will be using Visual Basic Express, a free version of Visual Studio focused on building VB.NET applications.Visual Basic Express

39、is an Integrated Development Environment (IDE) because it is composed of various tools, menus and toolbars which ease the creation and management of your code.The project system in Visual Basic Express comprises Solution and Project files as well as Project Items, the individual files belonging to p

40、rojects. A solution is a container for one or more projects. Each project can in turn be considered a container for project items such as source files, icons, etc. most of which get compiled into the resultant executable file (EXE or DLL). Visual Basic Express provides a Solution Explorer that organ

41、izes and displays the contents of the loaded solution in a tree-view format:The Visual Basic Express interface also contains a text editor and an interface designer. These are displayed in the main window depending on the type of file being edited. The text editor is where you will enter the Visual

42、Basic code for your Inventor plug-in. This editor provides advanced features such as IntelliSense and collapsible code sections along with the more classic text-editing features such as bookmarks and the display of line numbers.IntelliSense is an extremely valuable feature of the Visual Studio famil

43、y that greatly improves programmer productivity: it automatically provides suggestions for the code being written based on the objects available and the letters that are being typed. IntelliSense showing the methods and properties for a ComponentOccurrence:Clearly one of the key features of Visual B

44、asic Express is its ability to build VB.NET code into an executable file. During the build process, the language compiler performs various checks and analyses on the code. One such check is to ensure the code conforms to the syntactical rules of the Visual Basic language. The compiler also performs

45、various other checks, such as whether a variable has been appropriately defined or not. Detected errors are reported via the Error List Window, typically found at the bottom of the main window. The Error List can be displayed by selecting Error List from the View menu Other Windows. The Visual in Vi

46、sual Basic Express One of the main strengths of Visual Basic Express is its set of tools for creating a User Interface. When you create a new project you can select a Windows Forms Application. When this template is used, the main window for the application is automatically created. This window is c

47、alled a form and you can place user interface elements (called controls), such as a command buttons, on it. You add an element simply by selecting it in the Toolbox and then clicking and dragging it onto the Form. The Toolbox can be displayed using the View menu Other Windows Toolbox. Much of the co

48、de to make these elements work correctly gets added automatically to the project, greatly reducing the effort needed to get your application up and running. Reviewing your use of Visual Basic Express In this section, you will create a blank project similar to the one you were provided in Lesson 1. This project will be the starting point for Lesson 3. 1. Create a Windows Forms Application:Close the project from Lesson 1, if it is still open in Visual Basic Express, and then on the File menu, click Ne

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1