top of page

A Full Guide To Internal Subroutines In PcDmis

  • Writer: Metis
    Metis
  • Nov 1, 2023
  • 4 min read

Updated: Nov 15, 2023

It can be difficult to predict behaviour when using subroutines, which isn't helpful when trying to program a CMM. The aim of this post is to helpfully clear things up and give people a better understanding of how to use them.

This is a big and confusing topic, I intend to keep adding to this blog as and when.

What is a Subroutine in PcDmis?

Subroutines allow the user to to write a block of code which can be used repeatedly within other programs, they can be written internally to be used by the current program or externally and referenced in another program. This post covers internal subroutines, I'll cover external subroutines in another post.

Alignments

Subroutines use the alignment that is active when the subroutine is called, for example...

A1         =ALIGNMENT/START,RECALL:STARTUP,LIST=YES
              ALIGNMENT/TRANS,XAXIS,PNT_MAN
              ALIGNMENT/TRANS,YAXIS,PNT_MAN
              ALIGNMENT/TRANS,ZAXIS,PNT_MAN
            ALIGNMENT/END
CS1        =CALLSUB/SUB_ALIGN1,:,

            RECALL/ALIGNMENT,INTERNAL,STARTUP
            SUBROUTINE/SUB_ALIGN1,
                 = 
PNT1       =FEAT/CONTACT/VECTOR POINT/DEFAULT,CARTESIAN
            THEO/<0,0,0>,<0,0,1>
            ACTL/<0,0,0>,<0,0,1>
            TARG/<0,0,0>,<0,0,1>
            SHOW FEATURE PARAMETERS=NO
            SHOW CONTACT PARAMETERS=NO
            ENDSUB/

The above program will measure PNT1 using alignment A1, even though the STARTUP alignment is recalled just about the subroutine.

Internal subroutines also have access to alignments created outside of the subroutine, for example...

A1         =ALIGNMENT/START,RECALL:STARTUP,LIST=YES
              ALIGNMENT/TRANS,XAXIS,PNT_MAN
              ALIGNMENT/TRANS,YAXIS,PNT_MAN
              ALIGNMENT/TRANS,ZAXIS,PNT_MAN
            ALIGNMENT/END
            RECALL/ALIGNMENT,INTERNAL,STARTUP
CS1        =CALLSUB/SUB_ALIGN1,:,

            SUBROUTINE/SUB_ALIGN1,
                 = 
            RECALL/ALIGNMENT,INTERNAL,A1   
PNT1       =FEAT/CONTACT/VECTOR POINT/DEFAULT,CARTESIAN
            THEO/<0,0,0>,<0,0,1>
            ACTL/<0,0,0>,<0,0,1>
            TARG/<0,0,0>,<0,0,1>
            SHOW FEATURE PARAMETERS=NO
            SHOW CONTACT PARAMETERS=NO
            ENDSUB/

Again, the above program will measure PNT1 using alignment A1, even though it was called using the STARTUP alignment and alignment A1 was created outside of the subroutine.

Also note that the main program will continue using the STARTUP alignment after running the subroutine, even though the subroutine recalled alignment A1.

Parameter Settings

Subroutines use the parameter settings from the main program where the subroutine is called, for example...

            RECALL/ALIGNMENT,INTERNAL,A1
            MODE/DCC
            PREHIT/3
CS1        =CALLSUB/SUB_TEST,:,

            MODE/MANUAL
            PREHIT/10
            SUBROUTINE/SUB_TEST,
                 = 
            RECALL/ALIGNMENT,INTERNAL,A1   
PNT1       =FEAT/CONTACT/VECTOR POINT/DEFAULT,CARTESIAN
            THEO/<0,0,0>,<0,0,1>
            ACTL/<0,0,0>,<0,0,1>
            TARG/<0,0,0>,<0,0,1>
            SHOW FEATURE PARAMETERS=NO
            SHOW CONTACT PARAMETERS=NO
            ENDSUB/

In the program above, PNT1 will be executed in DCC mode used a pre-hit of 3, even though they're set differently just above the subroutine.

If a parameter setting is changed within the subroutine, the changes only applies within the subroutine. The main program will continue to use the parameter settings applied before the subroutine was called.

Active Probes/Tips

Similar to the parameter settings, subroutines use the the active probe and tip from the main program where the subroutine was called. For example...

            RECALL/ALIGNMENT,INTERNAL,A1
            TIP/T1A0B0, SHANKIJK=0, 0, 1, ANGLE=0
CS1        =CALLSUB/SUB_TEST,:,

            TIP/T1A90B0, SHANKIJK=0, 1, 0, ANGLE=180
            SUBROUTINE/SUB_TEST,
                 = 
            RECALL/ALIGNMENT,INTERNAL,A1   
PNT1       =FEAT/CONTACT/VECTOR POINT/DEFAULT,CARTESIAN
            THEO/<0,0,0>,<0,0,1>
            ACTL/<0,0,0>,<0,0,1>
            TARG/<0,0,0>,<0,0,1>
            SHOW FEATURE PARAMETERS=NO
            SHOW CONTACT PARAMETERS=NO
            ENDSUB/

In the program above, PNT1 will be executed with the tip angle T1A0B0, even though they're set differently just above the subroutine.

Something to be wary of...

            RECALL/ALIGNMENT,INTERNAL,A1
            TIP/T1A0B0, SHANKIJK=0, 0, 1, ANGLE=0
CS1        =CALLSUB/SUB_TEST,:,
PNT2       =FEAT/CONTACT/VECTOR POINT/DEFAULT,CARTESIAN
            THEO/<0,0,0>,<0,0,1>
            ACTL/<0,0,0>,<0,0,1>
            TARG/<0,0,0>,<0,0,1>
            SHOW FEATURE PARAMETERS=NO
            SHOW CONTACT PARAMETERS=NO
            ENDSUB/

            SUBROUTINE/SUB_TEST,
                 = 
            RECALL/ALIGNMENT,INTERNAL,A1
            TIP/T1A90B0, SHANKIJK=0, 1, 0, ANGLE=180
PNT1       =FEAT/CONTACT/VECTOR POINT/DEFAULT,CARTESIAN
            THEO/<0,0,0>,<0,0,1>
            ACTL/<0,0,0>,<0,0,1>
            TARG/<0,0,0>,<0,0,1>
            SHOW FEATURE PARAMETERS=NO
            SHOW CONTACT PARAMETERS=NO
            ENDSUB/

In the program above, the tip angle is changed within the subroutine. PNT1 will be measured using tip angle T1A90B0, however after the subroutine is called, the tip angle will physically remain at T1A90B0 but the main program will carry on to measure PNT2 as though as the tip is still at T1A0B0 (it won't physically move back).

Because of this, I would recommend wither not ever changing probes/tips within a subroutine, or making sure the correct tip/probe is called back after a CALLSUB command.

Variables/Assignments

Variables assigned within the main program are not visible to subroutines unless they are passed as a parameter. For example...

            ASSIGN/FOO=100
CS1        =CALLSUB/SUB_TEST,:,

            SUBROUTINE/SUB_TEST,
                 = 
            COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
            FOO
            ENDSUB/

The comment in the program above will return 0 because variable FOO is not visible to the subroutine.

However, variable can be passed to the subroutine as parameters like in the following example...

            ASSIGN/FOO=100
CS1        =CALLSUB/SUB_TEST,:FOO,,

            SUBROUTINE/SUB_TEST,
                FOO2 = 0 : ,
                 =
            COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
            FOO2
            ENDSUB/

In the the program above, the subroutine accepts a parameter and names it FOO2,

The comment in the program above will return 100 because it uses the first parameter which has been assigned when it was called in the main program.

Any changes made to the parameters passed to the subroutine are kept, for example...

            ASSIGN/FOO=100
CS1        =CALLSUB/SUB_TEST,:FOO,,
            COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
            FOO

            SUBROUTINE/SUB_TEST,
                FOO2 = 0 : ,
                 =
            ASSIGN/FOO2=200
            ENDSUB/

In the program above, the comment would return 200 because FOO was passed to the subroutine as a parameter, and the subroutine changed the parameter to 200.

Any assignments created in a subroutine are not visible to the main program, for example...

CS1        =CALLSUB/SUB_TEST,:,
            COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
            FOO

            SUBROUTINE/SUB_TEST,
                 =
            ASSIGN/FOO=100
            ENDSUB/

In the example above, the comment in the main program returns 0 because it was assigned in the subroutine.

Features

Feature references are a bit strange when using internal subroutines, you have to be careful with naming.

Features in the main program can be referenced in the subroutine using either the parameter name or its original name as soon as it's passed as a parameter...

PNT1       =FEAT/CONTACT/VECTOR POINT/DEFAULT,CARTESIAN
            THEO/<0,0,0>,<0,0,1>
            ACTL/<10,0,0>,<0,0,1>
            TARG/<0,0,0>,<0,0,1>
            SHOW FEATURE PARAMETERS=NO
            SHOW CONTACT PARAMETERS=NO
CS1        =CALLSUB/SUB_TEST,:{PNT1},,

            SUBROUTINE/SUB_TEST,
                V1 =  : ,
                 = 
            COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
            PNT1.X
            ENDSUB/

In the example above, the comment returns 10. The same result would have been achieved if parameter V1 was used in the comment. If PNT1 was not included as a parameter in the subroutine then the result would have been 0.

Features created in the subroutine are always accessible to the main program, for example...

CS1        =CALLSUB/SUB_TEST,:
            COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
            PNT1.X
            RECALL/ALIGNMENT,INTERNAL,STARTUP
            
            SUBROUTINE/SUB_TEST,
                 = 
PNT1       =FEAT/CONTACT/VECTOR POINT/DEFAULT,CARTESIAN
            THEO/<0,0,0>,<0,0,1>
            ACTL/<10,0,0>,<0,0,1>
            TARG/<0,0,0>,<0,0,1>
            SHOW FEATURE PARAMETERS=NO
            SHOW CONTACT PARAMETERS=NO
            ENDSUB/

The commend in the main program would return 10 even though PNT1 is not passed as a parameter. Be careful not to overwrite duplicate features!

Features in the program can also be edited using a subroutine like in the example below...

PNT1         =GENERIC/POINT,DEPENDENT,CARTESIAN,$
            NOM/XYZ,<0,0,0>,$
            MEAS/XYZ,<0,0,0>,$
            NOM/IJK,<0,0,1>,$
            MEAS/IJK,<0,0,1>
CS1        =CALLSUB/SUB_TEST,:{PNT1},,

            SUBROUTINE/SUB_TEST,
                V1 =  : ,
                 = 
            ASSIGN/PNT1.XYZ=MPOINT(1,2,3)
            ENDSUB/

In the above example, the subroutine would edit the actual values generic point PNT1 to X=1, Y=2, Z=3.

Dimensions could also be updated but it doesn't update the report so it isn't really useful, it's better to edit a generic feature then then dimension that.


That's all I've got for now, please add any suggestions to improve the guide in the comments below.



Recent Posts

See All
Using functions in PcDmis

If you need to carry out a complex calculation or operation multiple times in your program, it might be beneficial to create a function...

 
 
 

Comments


Metis Metrology Ltd | Registered Company No: 13456045

Metis Metrology Ltd, Lychett House, 13 Freeland Park, Wareham Road, Poole, Dorset, BH16 6FA, United Kingdom

bottom of page
Privacy Policy