How to make this code better?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Just wondering how I can make some code better. Would it be better off to use a new class and split the matrix off, or should I just use the same 'class' it is in now? I'm thinking it's probably best to split the for loops off into 2 subs as well and pass arguements, but I'm also wondering what else I need to do to make this as strong a program as possible, program wise.
I've been looking up a lot lately on VB.net and good programming practices, but other opinions are definitely always great
(Tip for new programmers: Look at the sPromptStrings string definition; the msdn website suggests using the inline format with the definition after the variable type as shown, rather than other variations.)
*This complete code iterates through sheets, reads titleblocks, and puts the information in a stacked sketched symbol on various sheets.
Imports System Imports System.Type Imports System.Activator Imports Inventor Imports System.Runtime.InteropServices Sub Main() Dim oInv_App As New CL_InventorApp Dim oDoc As Document = oInv_App.ActiveDoc Dim oTransActmanager As TransactionManager = oInv_App.InventorInstance.TransactionManager Dim oTransaction As Transaction = oTransActmanager.StartTransaction(oDoc, "Bulk Add Revision Row") AddRefLabels oTransaction.End End Sub Sub AddRefLabels() Dim oInv_App As New CL_InventorApp Dim oDoc As Document = oInv_App.ActiveDoc Dim oTitleBlock As TitleBlock Dim oTextBox As TextBox Dim oSheet As Sheet Dim oView As DrawingView Dim oModel As Document Dim oModelName As String Dim oDocName As String Dim oDesc As String Dim oDwgNo As String Dim oSketchedSymbolDef As SketchedSymbolDefinition = oDoc.SketchedSymbolDefinitions.Item("Reference Drawing") Dim oSketched As SketchedSymbol Dim oTG As TransientGeometry = ThisApplication.TransientGeometry Dim oInsertionPoint As Point2d Static Matrix1(0 To 3, 0 To oDoc.Sheets.Count) As String For j=1 To oDoc.Sheets.Count oSheet=oDoc.Sheets.Item(j) oModelName = oSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument.FullDocumentName Matrix1(1, j)= oModelName oTitleBlock = oSheet.TitleBlock For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes Select Case oTextBox.Text Case "CUSTOMER DRAWING#" oDwg=oTitleBlock.GetResultText(oTextBox) Case "TITLE" oTitle= oTitleBlock.GetResultText(oTextBox) Case "DESCRIPTION 1" oDesc1= oTitleBlock.GetResultText(oTextBox) Case "DESCRIPTION 2" oDesc2= oTitleBlock.GetResultText(oTextBox) End Select If oDesc2 = "" Or oDesc2 = "-" Then oDesc=oTitle & " - " & oDesc1 Else oDesc=oDesc1 & " - " & oDesc2 End If Next Matrix1(2,j)= oDesc Matrix1(3,j)= oDwg Next For Each oSheet In oDoc.Sheets Dim p As Integer = 0 oModel = oSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument For m=1 To oModel.ReferencingDocuments.Count For n=1 To oDoc.Sheets.Count If oModel.ReferencingDocuments.Item(m).FullFileName = Matrix1(1,n) oInsertionPoint= oTG.CreatePoint2d(68.3246980561765,7.20598000000001+(7.77748000000001-7.20598000000001)*p) Dim sPromptStrings As String() = {Matrix1(2,n), Matrix1(3,n)} oSheet.SketchedSymbols.Add(oSketchedSymbolDef,oInsertionPoint, 0, 1, sPromptStrings) p=p+1 End If Next Next Next End Sub Public Class CL_InventorApp Dim Public InventorInstance As Inventor.Application Dim Public ActiveDoc As Inventor.Document Dim _started As Boolean Public Sub New() Try InventorInstance = Marshal.GetActiveObject("Inventor.Application") ActiveDoc = InventorInstance.ActiveDocument Catch ex As Exception Try Dim invAppType As Type = GetTypeFromProgID("Inventor.Application") InventorInstance = CreateInstance(invAppType) InventorInstance.Visible = True ActiveDoc = InventorInstance.ActiveDocument _started = True Catch ex2 As Exception MsgBox(ex2.ToString()) MsgBox("Unable to get or start Inventor") Exit Sub End Try End Try End Sub End Class
--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization
iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread
Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects
Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help
Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type