Inventor二次开发入门.docx
《Inventor二次开发入门.docx》由会员分享,可在线阅读,更多相关《Inventor二次开发入门.docx(75页珍藏版)》请在冰豆网上搜索。
Inventor二次开发入门
Inventor二次开发入门
课程1:
开始接触InventorAPI,用VB.NET写一个最简单的插件,实现选择集的隐藏
在本课中,你将开始写一个使用AutodeskInventorAPI基于VB.NET隐藏所选组件的应用程序。
演示视频(英文)
演示代码
lesson1_vb-net.zip(zip-49Kb)
lesson1_c-sharp.zip(zip-73Kb)
根据步骤来创建你的第一个插件
1.LaunchtheVisualBasicExpressdevelopmentenvironment:
OpenVisualBasic2010ExpressusingtheWindowsStartmenu,selectingAllPrograms,thenMicrosoftVisualStudio2010Express,andthenMicrosoftVisualBasic2010Express.Note:
YoucanalsouseVisualBasic2008Expresswiththisguide.Projectsforboth2010and2008areprovided.
2.Openaclasslibraryproject:
InsideVisualBasicExpress,ontheFilemenu,clickOpenProject.Navigatetothesubfolderofthesupportingmaterialyoudownloadedatthetopofthisguidecalledlesson1_VisualExpress2010andopentheVB.NETprojectcontainedwithinitbyselectingtheprojectfileMyFirstInventorPlugin_Lesson1.vbproj.
3.Openthecode:
Intheopenprojectyouwillseeaformwithonebutton(ifyoudon’tseetheForm,clickonForm1.vbintheSolutionExplorerframeintheupperrighthandside).RightclickonForm1intheSolutionExplorerandselectViewCodeorjustdoubleclickontheForm.
4.Addthecode:
Inthecodewindow,typethecodebelowintotheSubButton1_Click.(Thisiswhatrunswhenthebuttonisclicked.) Youmayneedtoscrolldowntowardsthebottomofthecodetofindtheplacetoaddthebelowcode,lookingforthewords‘AddcodeforLesson1here’. TogetthefullexperienceofdevelopingwithVisualBasicExpress–includingtheuseoffeaturessuchasIntelliSense–werecommendyoutypethecodefromthisguideratherthancopyingandpastingit.Thatsaid,ifconstrainedfortimeyoucanalsocopyandpasteintotheVisualBasicExpresscodewindow,althoughthiswillreducetheexperienceyougainfromworkingwiththecodedirectly.
If_invApp.Documents.Count=0Then
MsgBox("NeedtoopenanAssemblydocument")
Return
EndIf
If_invApp.ActiveDocument.DocumentType<>_
DocumentTypeEnum.kAssemblyDocumentObjectThen
MsgBox("NeedtohaveanAssemblydocumentactive")
Return
EndIf
DimasmDocAsAssemblyDocument
asmDoc=_invApp.ActiveDocument
IfasmDoc.SelectSet.Count=0Then
MsgBox("NeedtoselectaPartorSubAssembly")
Return
EndIf
DimselSetAsSelectSet
selSet=asmDoc.SelectSet
Try
DimcompOccAsComponentOccurrence
DimobjAsObject
ForEachobjInselSet
compOcc=obj
Debug.Print(compOcc.Name)
compOcc.Visible=False
Next
CatchexAsException
MsgBox("IstheselecteditemaComponent?
")
MsgBox(ex.ToString())
Return
EndTry
5.Savethefile:
OntheFilemenu,clickSaveAll.
6.Buildtheproject:
Thecodeyouhavewrittenisinhumanreadableform.Tomakethecodereadablebyacomputer,youwillneedtotranslateitor“build”it.
InsideVisualBasicExpress,inthe Debug menu,click BuildSolution tocompileandbuildyourplug-in. The“BuildSuccess”messageshowsinstatusbaroftheVisualBasicExpresswindowifthecodeissuccessfullybuilt.
That’sit!
Youhavejustwrittenyourfirstplug-inforAutodeskInventor.Let’sruntheplug-intoseewhatitdoes.
RunningthePlug-in
1.StartAutodeskInventor.(Note:
Whentheplug-inisrunitwillstartanewsessionofInventorifoneisnotalreadyopen.)
2.CreateoropenanexistingInventorassembly:
EitherunzipthefileClutch_Bell_Simplified.zip,andopentheClutch_Bell_Simplified.iamassemblyorwithinInventormakesureyouhaveanassemblyofyourchoosingactive. ThereareseveraltypesofdocumentthatcanbecreatedorworkedwithinsideInventor.ThemostcommonlyuseddocumenttypesarePart(.ipt),Assembly(.iam)andDrawing(.idw).OpenanewassemblyandplacesomepartsusingthestandardInventoruser-interface.
3.Runyourplug-inwithInventorandallowtheplug-intocommunicatewithInventor:
TomakeVisualBasicExpressexecutethecodeyouhaveentered,selectStartDebuggingontheDebugmenu(youcanusetheF5keyorclickonthegreenarrow–whichlookslikea“play”button–ontheDebuggingtoolbar).Thiswillcauseyourformtobedisplayed.YoumayneedtominimizeVBExpresstoseeboththeformandInventor.
4.Workwiththeplug-in:
Selectoneormore(byusingtheCtrlkey)componentsintheassemblythatisactiveinsideInventorandthenclickButton1ontheformtoexecuteyourcodeandhidetheselectedcomponents.
5.Tore-displaytheinvisiblecomponentsusetheInventorAssemblybrowser(youcanidentifythemviatheircomponenticons,whichshouldnowbegrayedout). Inthebrowser,right-clickontheinvisiblecomponentsandpickVisibility,makingthemvisibleonceagain.
Congratulations!
Youhavejustwrittenyourfirstplug-inforAutodeskInventor.YouwillbereviewingthecodeindetailinLesson3.
Beforeyoumoveontothenextlessons,letusgobacktosomeofthethingsweskippedoverearlier,startingwithbasicconceptsaboutprogramming,andthebenefitsitcanbringtoyourday-to-daywork.
AdditionalTopics
IntroductiontoProgramming
TheVB.NETcodeyouhavejustexecutedthathidestheselectedcomponentsisonly30lineslongandmorethanhalfofthecodethatyouenteredintotheprojectisdoingerrorchecking.Thecodethatactuallydoestheworkcanbenarroweddowntothesefewlinesofcode:
DimasmDocAsAssemblyDocument
asmDoc=_invApp.ActiveDocument
DimselSetAsSelectSet
selSet=asmDoc.SelectSet
DimcompOccAsComponentOccurrence
DimobjAsObject
ForEachobjInselSet
compOcc=obj
compOcc.Visible=False
Next
Asyoucansee,asmallamountofcodecangoalongwaytosimplifyworkingwithInventor.Softwareprogrammingallowsyoutocapturethelogicofaparticularmanualprocedureonceandthenreapthebenefitsoverandoveragain,everytimeyouwanttoperformthisfunctionality.
WhatisProgramming?
Asimpleanswertothisquestionis:
Computerprogrammingistheprocessofcreatingasequenceofinstructionstotellthecomputertodosomething.Youcanlookatyourprogramasasequenceofinstructions.Duringthecourseoftheupcominglessons,youwilllookatthevariouslinesandblocksofcodeinthecontextofbeinginstructionsforacomputer.
Ifyouweretoexplainwhatcomputersaretoayoungchild,youmightsay:
acomputerisatoolthatfollowsinstructionsyouprovide.Programmingisonewayofgivinginstructionstothecomputer.Internally,acomputerseestheseinstructionsencodedasaseriesofnumbers(alsocalledmachinecode).Thehuman-readableinstructionsyousawatthebeginningofthislessonarecalledsourcecodeandthecomputerconvertstheseinstructionsintomachinecodewhichitcanthenreadandexecute.Asequenceofsuchinstructions(orcode),writtentoperformaspecifictask,iscalledaprogramandacollectionofsuchprogramsandrelateddataiscalledsoftware.AutodeskInventorisonesuchsoftwareproduct.
Sourcecodecanbewrittenindifferentlanguages,justashumansusedifferentlanguagestocommunicatebetweenourselves.ThelanguageyouwillbeusinginthisguideiscalledVisualBasic.NET(VB.NET).
WhatisanAPI?
APIistheacronymforApplicationProgrammingInterface:
thewayasoftwareprogrammercancommunicatewithasoftwareproduct.Forinstance,theInventorAPIisthewayprogrammerscanworkwithInventor,andestablisheswhatfunctionalityasoftwareprogrammercanusewithinInventor.SuchastheInventorAPIallowsyoutowriteinstructionsforInventortoexecuteoneaftertheother.
Puttingthisslightlydifferently:
commercialsoftwarecompanies,suchasAutodesk,oftendistributeasetoflibrariesthatyoucanuseinyourownprogramtointeractwithaparticularsoftwareproduct,suchasAutodeskInventor,andextenditsfunctionality.Thissetoflibrariesisknownasthesoftwareproduct’sAPI.
ThetypeofprogramyouwritetointeractwithasoftwareproductandextenditsfunctionalitywilldependuponhowtheAPIhasbeendesignedandwhathasbeenexposed(throughAPIs)foryoutoworkwith.
WhatisaPlug-in?
Asoftwareplug-inisatypeofprogrammodule(orfile)thataddsfunctionalitytoasoftwareproduct,usuallyintheformofacommandautomatingataskorsomecustomizationoftheproduct’sbehavior.Whenyoutalkaboutaplug-inforInventor–andyouwillalsohearthetermAddInorApplicationusedforthisproduct–wemeanamodulecontainingcodethatmakesuseoftheInventorAPI.ThecodecanconnecttoInventortoautomatetasks,orbeloadedbyInventorandusedtoadjustitsbehaviorofInventorundercertainconditions,suchaswhenaparticularcommandisexecutedbytheuseroftheplug-in.
Forterminologypurposes,anInventorAddInwouldalsobeconsideredaplug-in.AnAddInisaspecialkindofplug-inthatautomaticallyloadswhenInventorisstarted,hashighperformanceandappearstotheusertobepartofInventor..
课程2:
帮助了解VisualStudio编程环境以及基本需要熟悉的方面
Inthepreviouslesson,yousawhowyoucanincreaseproductivityinAutodeskInventorbyimplementingaplug-inbuiltfromasmallamountofVB.NETcode.
YouwillprobablyhaveheardthetermsCOMand.NETfromLesson1withreferencetoprogrammingwithInventor.BothCOMand.NETaretechnologiesthatenablecommunicationbetweensoftware:
ifyouareinterestedinlearningmore,youwillfindinformationintheAdditionalTopicssectionhere.
Youwillnowlookmorecloselyatwhathappenedwhenyouexecutedthecodeinthepreviouslesson.
代码示例
lesson2_vb-net.zip(zip-19Kb)
lesson2_c-sharp.zip(zip-23Kb)
Whatdoesitmeanto“build”code?
ThecodethatyoutypedintoVisualBasicExpressinLesson1wasasetofhuman-readableinstructions(sourcecode)thatneededtobeconvertedintocodethatc