How to make this code better?

How to make this code better?

MechMachineMan
Advisor Advisor
385 Views
2 Replies
Message 1 of 3

How to make this code better?

MechMachineMan
Advisor
Advisor

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.

Justin K
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
0 Likes
386 Views
2 Replies
Replies (2)
Message 2 of 3

ekinsb
Alumni
Alumni

Nothing jumps out at me as something that could be improved.  The main reason to break the code up into seperate functions is for re-use.  With what you here, I don't see any sections of code being repeated in other areas.  Another reason is to encapsulate some somewhat complex logic so you can develop and test that by itself and then use it in other places as a "black box".  I think your code is already simple enough so that breaking it up would affect the readability which impacts the ability to maintain the code.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3

MechMachineMan
Advisor
Advisor

For making .exe's or .dll's or even vb.net rules for inventor, what are your thoughts on just declaring shared variables rather than creating new class instances?

It seems like it would make sense to just use it with access modifiers, but at the same time, I'm hesitant as I dont such a practice used a lot.

 

Also, is there much concern ever of writing a vb.net program that would inadvertently crash your computer? Is there certain things code should all include if it is to be used as an .exe or .dll? Things such a memory purge or closing applications? Or is that mostly taken care of by whatever else the computer does with the application?


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
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
0 Likes