Author Topic: Can I pass different parameters to a function in one command?  (Read 2814 times)

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
Can I pass different parameters to a function in one command?
« on: August 26, 2012, 08:20:48 AM »
Hi.
I have defined a function which can accept different strings. This function will do differents works when different strings is passed. So I defined to commands.
Code: [Select]
<CommandMethod("AddOne")> _
    Public Sub AddOne()
        '' Get the current database and start the Transaction Manager
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database

        Dim pPtRes As PromptPointResult
        Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")

        pPtOpts.Message = vbLf & "Select the insert point:"
        pPtRes = acDoc.Editor.GetPoint(pPtOpts)
        Dim ptStart As Point3d = pPtRes.Value

        '' Exit if the user presses ESC or cancels the command
        If pPtRes.Status = PromptStatus.Cancel Then Exit Sub

        '' Start a transaction
        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            AddMasonry(ptStart, "1")        'This is the function I need to pass in different parameters
            acTrans.Commit()
        End Using

    End Sub

'The second command,just the parameter is different, all the other codes are the same?
<CommandMethod("AddTwo")> _
    Public Sub AddTwo()
        '' Get the current database and start the Transaction Manager
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database

        Dim pPtRes As PromptPointResult
        Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")

        pPtOpts.Message = vbLf & "Select the insert point:"
        pPtRes = acDoc.Editor.GetPoint(pPtOpts)
        Dim ptStart As Point3d = pPtRes.Value

        '' Exit if the user presses ESC or cancels the command
        If pPtRes.Status = PromptStatus.Cancel Then Exit Sub

        '' Start a transaction
        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            AddMasonry(ptStart, "2")        'The only difference is "2" is here, not the "1".
            acTrans.Commit()
        End Using

    End Sub
So, I question is can I pass the parameter to the function in one command.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Can I pass different parameters to a function in one command?
« Reply #1 on: August 26, 2012, 08:48:23 AM »
Hi,

You can factorize your code (extract a method): define a private Sub with one argument (of type String) that you call from your commands

Code - vb.net: [Select]
  1.     Private Sub Add(ByVal num As String)
  2.         '' Get the current database and start the Transaction Manager
  3.         Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  4.         Dim acCurDb As Database = acDoc.Database
  5.  
  6.         Dim pPtRes As PromptPointResult
  7.         Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
  8.  
  9.         pPtOpts.Message = vbLf & "Select the insert point:"
  10.         pPtRes = acDoc.Editor.GetPoint(pPtOpts)
  11.         Dim ptStart As Point3d = pPtRes.Value
  12.  
  13.         '' Exit if the user presses ESC or cancels the command
  14.         If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
  15.  
  16.         '' Start a transaction
  17.         Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  18.             AddMasonry(ptStart, num) ' <- Pass the Sub argument here.
  19.             acTrans.Commit()
  20.         End Using
  21.  
  22.     End Sub
  23.  
  24.     <CommandMethod("AddOne")> _
  25.     Public Sub AddOne()
  26.         Add("1") ' <- Call Add with "1" as argument
  27.     End Sub
  28.  
  29.     <CommandMethod("AddTwo")> _
  30.     Public Sub AddTwo()
  31.         Add("2") ' <- Call Add with "2" as argument
  32.     End Sub
Speaking English as a French Frog

waterharbin

  • Guest
Re: Can I pass different parameters to a function in one command?
« Reply #2 on: August 26, 2012, 09:05:32 AM »
Hi,gile.
 You solution is much better,but I was wondering could I do this with just one commond?

thanhhuynh

  • Guest
Re: Can I pass different parameters to a function in one command?
« Reply #3 on: August 26, 2012, 09:52:01 AM »
Hi, did you try using LispFunction
Code - C#: [Select]
  1. [LispFunction("DoubleMe")]
  2. public Object DoubleMe_LispFunction(ResultBuffer rb)
  3.  

waterharbin

  • Guest
Re: Can I pass different parameters to a function in one command?
« Reply #4 on: August 27, 2012, 02:22:12 AM »
Hi,thanhhuynh.
I don't know LispFunction. How to use it, can you give more details?

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

thanhhuynh

  • Guest
Re: Can I pass different parameters to a function in one command?
« Reply #6 on: August 27, 2012, 11:49:16 AM »
Thank, sirneb.
Hi 闻仲, you can try other link from spiderinnet1 to clarify: http://spiderinnet1.typepad.com/blog/2012/03/autocad-net-define-lisp-functions-input-and-output.html

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Can I pass different parameters to a function in one command?
« Reply #7 on: August 27, 2012, 12:21:23 PM »
Nice!

Not to detract from its obvious value of showing off the LispFunction decorator. Just one thing I might alter in that sample. Instead of all those if ... else if ... else if ... statements I might want to combine them into a switch structure:
Code - C#: [Select]
  1. Object DoubleValue(TypedValue tv)
  2. {
  3.     Object ret = null;
  4.    
  5.     switch (tv.TypeCode)
  6.     {
  7.         case (int)LispDataType.T_atom:
  8.             ret = true;
  9.             break;
  10.         case (int)LispDataType.Int16:
  11.             ret = ((Int16)tv.Value) * 2;
  12.             break;
  13. // Continued
IMO it's a bit easier to "read", but more importantly the switch gives a more optimal compiled code.

Another alternative might be to use overloaded versions of the DoubleValue function. But I'm not too sure if such would be much more efficient. It might be a bit too much typing  ;)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.