Results 1 to 7 of 7

Thread: Visual Basic for Inventor 2016

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    3

    Visual Basic for Inventor 2016

    I am totally new to these forums and almost as new to trying to code as well. So if I am in the wrong spot just let me know where to go, it wasn't intentional.

    What I am trying to do is this:

    I have a part number (.ipt) file with the file name 9999-ABCDEF. I used the following code to isolate each extension and place them in an array. I've also defined the values that each of the extension letters tie to, and have placed their variants into arrays as well. For example, the first extension is the letter "A". This extension is tied to the LengthVariants array which should drive the part length to be 12". If the second extension is "B" which (being tied to the HeightVariants array) should drive the part height should be 3".

    My plan was to have each letter of the extension convert to a number that I then use as my reference point for pulling the dimension value from the secondary arrays. ("A" would convert to 0 and the code would search LengthVariants(0) to get 12".

    However I cannot figure out the way to convert the letter codes from my first array into numbers that I can use as index points for my secondary arrays. How can I replace characters with numbers in an array?

    Code:
    'FileName
    Name=ThisDoc.FileName(False)
    
    'Dividing Symbol
    Delimiter = InStr (Name,"-")
    
    'The Count of Extensions past the Delimiter
    ExtensionLength = Len(Name)-Delimiter
    
    'Setting Array That Holds The Characters of all the extensions
    Dim Extensions(ExtensionLength-1) As String
    
    'The Next Count/While Loop Sets the Specific Values in the array based off the extension E(0) will be the first extension, E(1) the second, etc.
    'TEST KEY'
    'Extensions(0)=LengthVariants Extension Letter
    'Extensions(1)=HeightVariants Extension Letter
    'Extensions(2)=FlangeVariants Extension Letter
    'Extensions(3)=FirstCutoutVariants Extension Letter
    'Extensions(4)=StrandCentersVariants Extension Letter
    'Extensions(5)=StrandQTYVariants Extension Letter
    Count = 0
    While Count < ExtensionLength
    Extensions(Count)=Left(Right(Name,Len(Name)-Delimiter-Count),1)
    Count = Count + 1
    End While
    'End of loop to seperate and assign each extension
    
    'Setting Array That Holds all the possible values of each extension
    LengthVariants = New Double(){12,24,36,48,60,72,84,96,108,120}
    HeightVariants = New Double(){2,3,4,5,6,6.5,7,8,9,10}
    FlangeVariants = New Double(){1,1.5,2,2.5,3}
    FirstCutoutVariants = New Double(){6,9,12,15,18,21,24,27,30}
    StrandCentersVariants = New Double(){6,9,12,15,18,21,24,27,30}
    StrandQTY = New Double(){1,2,3,4,5,6}
    
    'Setting Array to Convert Letter Code into index reference point for above arrays
    '(Cant Figure this part out)
    Last edited by TheMechEngineer; Oct 17th, 2019 at 04:42 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Visual Basic for Inventor 2016

    Concurrent arrays are, by definition, arrays where elements at the same index are related. If you want to translate from a value in one array to a value in the other by that relationship then you do it by index. If you have a value in the first array then you need to find its index in that array. You then use that index to get the related value from the second array.
    vb.net Code:
    1. Dim index = Array.IndexOf(firstArray, originalValue)
    2. Dim relatedValue = secondArray(index)
    I think that answers the question you're asking.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Visual Basic for Inventor 2016

    So, the extension isn't always six letters, but if shorter than six letters, the letters apply in order to the decode arrays?

    example:
    123-CCBD

    You would expect :
    LengthVariant = 36
    HeightVariant = 4
    FlangeVariant = 1.5
    FirstCutoutVariant = 15

    As a test of your type of code with jmc's information, I tried the following.
    The first two items were retrieved using an array Extensions as you had.
    But since you can access the characters of a string directly, there actually wouldn't be a reason to loop and separate those out. Just step through the extension substring part of the name string.

    The second two items are fetch by accessing the character position in the extension string as an example of that.
    I just put the test code in a button and set a breakpoint and stepped through the code to verify the correct values were being pulled from the array, so if you just run the code you can't tell that it work, you have to step through or add some output.
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim keys As String = "ABCDEFGHIJK"
    
        Dim LengthVariants = New Double() {12, 24, 36, 48, 60, 72, 84, 96, 108, 120}
        Dim HeightVariants = New Double() {2, 3, 4, 5, 6, 6.5, 7, 8, 9, 10}
        Dim FlangeVariants = New Double() {1, 1.5, 2, 2.5, 3}
        Dim FirstCutoutVariants = New Double() {6, 9, 12, 15, 18, 21, 24, 27, 30}
        Dim StrandCentersVariants = New Double() {6, 9, 12, 15, 18, 21, 24, 27, 30}
        Dim StrandQTY = New Double() {1, 2, 3, 4, 5, 6}
    
        Dim Extensions = New String() {"C", "C", "B", "D"}
    
        Dim LengthVariant As Double = LengthVariants(keys.IndexOf(Extensions(0)))
        Dim HeightVariant As Double = HeightVariants(keys.IndexOf(Extensions(1)))
    
        Dim extension As String = "CCBD"
        Dim FlangeVariant As Double = FlangeVariants(keys.IndexOf(extension(2)))
        Dim FirstCutoutVariant As Double = FirstCutoutVariants(keys.IndexOf(extension(3)))
    
      End Sub
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    3

    Re: Visual Basic for Inventor 2016

    I appreciate the help guys, it may be awhile before I respond. That is because I don't actually understand all that you've said or the code that you've posted. So I need time to research things. Once I have had time to get a better grasp I may come back and ask some questions of why/how.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    3

    Re: Visual Basic for Inventor 2016

    Okay I have a few questions but I'll start with the most simple. In looking at jmcilhinneys code, it appears index can be used to locate the index location of a value within an array.

    BUT

    From studying passels code the ".indexof" is referencing a string not an array. So I tried recreating this command to better understand it using both a string and an array and got an error when trying to have it reference an array. See Below.

    Code:
    Dim Key1 as String = "dfaf12sadfasF"
    A=Key1.Indexof(12)
    MessageBox.Show(A, "Title")
    The result of this is a message box that displays the number 4.

    However, when I try to recreate something similar with an array and not a string (as shown below) I get an error.

    Code:
    Key2 = New Integer(){3,6,9,12}
    B=Key2.Indexof(12)
    MessageBox.Show(B, "Title")
    The Error I get is

    "Error in rule: Build Array 2.0, in document: 9999-JFBAAA.ipt

    Overload resolution failed because no accessible 'Indexof' accepts this number of arguments."

    What am I doing wrong here, can indexof be used to find the position of a value within an array or is it only for strings?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Visual Basic for Inventor 2016

    What you're doing wrong is ignoring how I demonstrated how to use the Array.IndexOf method. Array.IndexOf and String.IndexOf are two different methods, defined in two different types. They are both intended to find the index of something within something else so they both have a name that indicates that but they are still different methods. String.IndexOf is an instance method of the String class so you call it on a String object. Array.IndexOf is a Shared method of the Array class so you call it on the Array class and pass it an array. If you want to find the index of an element in an array then call Array.IndexOf and I've shown you how to do that.

    Because all the values in their example are single characters, passel has used a String containing those characters and can thus use String.IndexOf. That's a bit less cumbersome than using an array and Array.IndexOf. If you're not dealing with single characters though, using a String is not an option.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Visual Basic for Inventor 2016

    That was my fault.
    Since I had changed to using String.IndexOf and knew it wasn't Array.IndexOf so wasn't an example of both methods that jmc mentioned, I was thinking I should point that out when I posted, but got called away so posted what I had and didn't get back to my computer until now. In my mind I was thinking that I was expanding on the response, i.e. showing a third method that could be used for part of this particular task, but since the third method had the same name as the first, knew that could be confusing and I should have pointed that out, but didn't.

    I commented out my use of String.IndexOf, and changed the Keys to an array of strings, and used the Array.IndexOf so you can see the difference in situ. Hopefully, you've already figured out the difference with jmc's additional guidance.
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Dim keys As String = "ABCDEFGHIJK"
        Dim keys As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"}
    
        Dim LengthVariants = New Double() {12, 24, 36, 48, 60, 72, 84, 96, 108, 120}
        Dim HeightVariants = New Double() {2, 3, 4, 5, 6, 6.5, 7, 8, 9, 10}
        Dim FlangeVariants = New Double() {1, 1.5, 2, 2.5, 3}
        Dim FirstCutoutVariants = New Double() {6, 9, 12, 15, 18, 21, 24, 27, 30}
        Dim StrandCentersVariants = New Double() {6, 9, 12, 15, 18, 21, 24, 27, 30}
        Dim StrandQTY = New Double() {1, 2, 3, 4, 5, 6}
    
        Dim Extensions = New String() {"C", "C", "B", "D"}
    
        'Dim LengthVariant As Double = LengthVariants(keys.IndexOf(Extensions(0)))
        'Dim HeightVariant As Double = HeightVariants(keys.IndexOf(Extensions(1)))
    
        Dim extension As String = "CCBD"
        'Dim FlangeVariant As Double = FlangeVariants(keys.IndexOf(extension(2)))
        'Dim FirstCutoutVariant As Double = FirstCutoutVariants(keys.IndexOf(extension(3)))
    
        Dim LengthVariant As Double = LengthVariants(Array.IndexOf(keys, Extensions(0)))
        Dim HeightVariant As Double = HeightVariants(Array.IndexOf(keys, Extensions(1)))
    
        Dim FlangeVariant As Double = FlangeVariants(Array.IndexOf(keys, extension(2)))
        Dim FirstCutoutVariant As Double = FirstCutoutVariants(Array.IndexOf(keys, extension(3)))
    
      End Sub
    p.s. Another nuance to these methods, is one method, Array.IndexOf is a Shared method, meaning the method is shared between all instances of array objects, so you don't use an instance of an array (the array's name) to access the method (e.g. keys), but use the Class name, "Array" in this case, for any instance, which is also why you need to pass the actual array instance you want to search to the method as the first argument.

    With the String,IndexOf, the method is not Shared, so is a member of each instance of String, so you use the object's name to call the method, e.g. keys.IndexOf. You don't pass the instance because the method is part of the instance so already "knows" what instance this is.
    Last edited by passel; Oct 19th, 2019 at 01:27 AM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width