Hi,

Thanks for the reply. I'm sorry if the problem appears vague. I tried to concoct the minimum code fragment that would illustrate my point. The code I posted wasn't copied from Mach3. It was typed in manually, so may well be wrong, though I can't see why.

But below I have the actual macro file that exhibits the problem I mentioned. If I merely move the declarations of the functions Max and Min to the end of the file it runs correctly. As it is, I get no error message when I press the green > button in the VB editor, but when my part file invokes it, no G code is output, so something is wrong.

My query about Elseif is exhibited by this file, too. It seems to execute correctly, but the word Elseif is black, not blue, in the editor.

The difference between Elseif and Else if is that elseif doesan't need its own end if.

I haven't surrounded my code with Sub Main...End Sub: Main as I can see no need to unless I want to exit prematurely.

None of the macros delivered with Mach3 uses Subs or Functions, so I can't see how others have done it.

My file M1000.m1s follows.

Thanks

Chris Lusby Taylor

'Macro for drawing a line clipped to x, y limits

'****** Revision 0.1 Feb 20 2008 *****

'Globals:

'#11=x min

'#14=x max

'#21=y min

'#24=y max

'#30 z safe

'#31 z engrave depth

'#15= border width

'

'Call with Pmmmm Qyyy Sccc where

'S=1 draw in X negative direction (am)

'S=2 draw in X positive direction (pm)

'S=3 position head at safe z on inner clip, X negative

'S=4 position head at safe z on inner clip, X positive

Option Explicit

Dim M,C,S,x1,x2,y1,y2 As Double

Function Max(ByVal a As Double, ByVal b As Double) As Double

If a>b Then Max=a Else Max=b

End Function

Function Min(ByVal a As Double, ByVal b As Double) As Double

If a>b Then Min=b Else Min=a

End Function

M = Param1()

C = Param2()

S = Param3()

Select Case S

Case 1,3

If M=0 Then

y1=C

y2=C

x2=GetVar(11)

x1=x2+GetVar(15)

Else

If M>0 Then

y1=Max(GetVar(21)+GetVar(15),(GetVar(11)+GetVar(15))*M+C)

x1=(y1-C)/M

y2=Max(GetVar(21),GetVar(11)*M+C)

x2=(y2-C)/M

Else

y1=Min(GetVar(24)-GetVar(15),(GetVar(11)+GetVar(15))*M+C)

x1=(y1-C)/M

y2=Min(GetVar(24),GetVar(11)*M+C)

x2=(y2-C)/M

End If

End If

Case 2,4

If M=0 Then

y1=C

y2=C

x2=GetVar(14)

x1=x2-GetVar(15)

ElseIf M<0 Then

y1=Max(GetVar(21)+GetVar(15),(GetVar(14)-GetVar(15))*M+C)

x1=(y1-C)/M

y2=Max(GetVar(21),GetVar(14)*M+C)

x2=(y2-C)/M

Else

y1=Min(GetVar(24)-GetVar(15),(GetVar(14)-GetVar(15))*M+C)

x1=(y1-C)/M

y2=Min(GetVar(24),GetVar(14)*M+C)

x2=(y2-C)/M

End If

End Select

Call Code("G0 X" & x1 & " Y" &y1)

If S<3 Then

Call Code ("G1 Z" & GetVar(31))

Call Code ("G1 X" & x2 &" Y" &y2)

Call Code ("G1 Z" & GetVar(30))

End If