Jump to content
Sign in to follow this  
MaxStudio

Predefine Function?

Recommended Posts

Is there a way to predefine a function and label it so I can call it when needed instead of retyping it over and over again?

Share this post


Link to post

You can create a file which has common functions and $include it into other scripts.

However care is needed to make sure that you do not create inter functional dependencies.

Share this post


Link to post

thanks Ian,

include would definitely work. Although, i'm looking to do the exact same thing as $include, but contain it all in a single file.

any ideas?

Thanks for your help.

derek

Share this post


Link to post

Are you saying you are using the same function over and over in the same file?

Share this post


Link to post

yes exactly

i guess it would be a procedure (sub-procedure) rather than a function.

Edited by MaxStudio

Share this post


Link to post

I got the following test to work using sub-procedures. I get confused about where to put things when there are so many end's... lol

Procedure ProSub;

VAR

id:LONGINT;

result:LONGINT;

gA, gB: BOOLEAN;

A,B,C,D: REAL;

FUNCTION DED_ProSub(VAR id:LONGINT):BOOLEAN;

BEGIN

{ initialize dialog creation }

id := CreateLayout('Procedure/Sub-Procedure',TRUE,'Create','Cancel');

{ Menu 1 }

CreateStaticText(id,4,'Type:',-1);

CreateCheckBox(id,5,'A.');

CreateCheckBox(id,6,'B.');

{ Layout Dialog }

SetFirstLayoutItem(id,4);

SetRightItem(id, 4, 5, 0, 0);

SetRightItem(id, 5, 6, 0, 0);

DED_ProSub:=VerifyLayout(id);

END;

PROCEDURE DD_ProSub(VAR item:LONGINT; data:LONGINT);

BEGIN; { SET DEFAULTS }

CASE item OF

SetupDialogC: BEGIN

SetItemEnable(5,TRUE);

SetItemEnable(6,TRUE);

END;

1:BEGIN

IF ItemSel(5) THEN gA:= TRUE ELSE gA:= FALSE;

IF ItemSel(6) THEN gB:= TRUE ELSE gB:= FALSE;

END;

END;

END;

{ Begin Define Sub-Procedures ------------------------------- }

Procedure Sub;

BEGIN

Rect(A,B,C*2,D*2);

END;

Procedure Sub2;

BEGIN

Rect(A,B,C*4,D*4);

END;

{ End Define Sub-Procedures -------------------------------- }

BEGIN

IF DED_ProSub(id) THEN BEGIN

result:=RunLayoutDialog(id,DD_ProSub);

END;

IF (result=1) THEN BEGIN

A:=0;

B:=0;

C:=50;

D:=50;

IF gA THEN BEGIN

Sub2;

END;

IF gB THEN BEGIN

Sub;

END;

END;

END;

RUN(ProSub);

Share this post


Link to post

To avoid confusion with begin and ends, use indentation. There are different styles with this, but all my commercial programming life, I tab (typically 8 spaces) indented after any begin and reverted back before the end.

eg. but only using 4 spaces. Programming style is largely personal style so I would format this differently than you have done, but there are some things that are good practice/corporate style rather than just personal style.

One thing you could have done is create a single function sub and passed a variable as a scaling factor.

Procedure ProSub;

VAR
   id      : LONGINT;
   result  : LONGINT;
   gA, gB  : BOOLEAN;
   A,B,C,D : REAL;

FUNCTION DED_ProSub(VAR id:LONGINT):BOOLEAN;
BEGIN
   { initialize dialog creation }
   id := CreateLayout('Procedure/SubProcedure',
TRUE,'Create','Cancel');

   { Menu 1 }
   CreateStaticText(id,4,'Type:',-1);
   CreateCheckBox(id,5,'A.');
   CreateCheckBox(id,6,'B.');

   { Layout Dialog }
   SetFirstLayoutItem(id,4);

   SetRightItem(id, 4, 5, 0, 0);
   SetRightItem(id, 5, 6, 0, 0);

   DED_ProSub:=VerifyLayout(id);
END;

PROCEDURE DD_ProSub(VAR item:LONGINT; data:LONGINT);
BEGIN { SET DEFAULTS }
   CASE item OF
       SetupDialogC:
       BEGIN
           SetItemEnable(5,TRUE);
           SetItemEnable(6,TRUE);
       END;

       1:
       BEGIN
           IF ItemSel(5) THEN gA:= TRUE ELSE gA:= FALSE;
           IF ItemSel(6) THEN gB:= TRUE ELSE gB:= FALSE;
       END;
   END;
END;

{ Begin Define Sub-Procedures ------------------------------- }

Procedure Sub;
BEGIN
   Rect(A,B,C*2,D*2);
END;

Procedure Sub2;
BEGIN
   Rect(A,B,C*4,D*4);
END;

{ End Define Sub-Procedures -------------------------------- }

BEGIN
   IF DED_ProSub(id) THEN
   BEGIN
       result:=RunLayoutDialog(id,DD_ProSub);
   END;

   IF (result=1) THEN
   BEGIN
       A:=0;
       B:=0;
       C:=50;
       D:=50;

       IF gA THEN
       BEGIN
           Sub2;
       END;

       IF gB THEN
       BEGIN
           Sub;
       END;
   END;
END;

RUN(ProSub);

Share this post


Link to post

Forgive me if you already know this, but have you looked at the VS Language Guide? It's available through the VW help system. There's a section called User Defined Functions that you may find useful.

-Josh

Share this post


Link to post

Hey Josh! Thanks a lot for pointing out that section in the language guide. "subroutines" is exactly what I was trying to accomplish.

Ian, thanks for your help. I'm going to try to keep my scripts more organized using the indentation method you have shared with us.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

 

7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114

 

© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.

×
×
  • Create New...