Jump to content
Mr. Klaus Straka

Strange behaviour of a function in feature definition

Recommended Posts

Hi,

 

i am working on a feature that actually draw some helical curves and circles. Therefor i frequently have to calulate an angle in the x-y-plane. Ok, thats easy. To get the right actual angle in the range of 0-360 degrees it wrote a function

function quadrantCorrection(FPoint startp, FDouble phi):FDouble
  if (AND(startp.getY()>=0,startp.getX()<0)) // 2th. Q.
    phi = 180-phi
  elseif (AND(startp.getY()>=0,startp.getX()>=0)) // 1st Q.
    phi = phi
  elseif (AND(startp.getY()<0,startp.getX()>=0)) // 4nd. Q.
    echo("Q4, phi " + phi)
    phi = 360-phi
    echo("Q4, phiCorr " + phi)
  elseif (AND(startp.getY()<0,startp.getX()<0)) // 3rd. Q.
    phi = 180+phi
  endif
  return(phi)
endfunction

I use this function for a great number of times and it seems to work fine. However in the attached project in the feature definition "MixerBasemesh" at line 340 it fails for whatever reason.

Below some debug output is given, the x and y coordinate show that the point is located in 4th quadrant. The function above detects this correctly (indicated by the debug output "Q4, phi 7.44226") but it does not compensate (phi=360-phi) the angle as shown by "Q4, phiCorr 7.44226".

 

phiE: 7.44226
x: 19.9307
y: -2.60349
Q4, phi 7.44226
Q4, phiCorr 7.44226
phiECorr: 7.44226
phi: -37.5577
phi: -37.5577
-----

This is a somwhat strange behaviour, i have no idea what i am doing wrong. Any comments on this.

I use V4.1.0 on Ubuntu.

 

Thank you for your help an best regards,

 

Klaus

Share this post


Link to post
Share on other sites

Hi Klaus,

 

Please try to change the code of the function to the following:

function quadrantCorrection(FPoint startp, FDouble phi):FDouble
  if (AND(startp.getY()>=0,startp.getX()<0)) // 2th. Q.
    phi = 180-#phi
  elseif (AND(startp.getY()>=0,startp.getX()>=0)) // 1st Q.
    phi = phi
  elseif (AND(startp.getY()<0,startp.getX()>=0)) // 4nd. Q.
    echo("Q4, phi " + phi)
    phi = 360-#phi
    echo("Q4, phiCorr " + phi)
  elseif (AND(startp.getY()<0,startp.getX()<0)) // 3rd. Q.
    phi = 180+#phi
  endif
  return(phi)
endfunction

It works for me like that. Apparently, there is a problem when trying to assign an expression to an argument inside a function that includes the argument itself. To be honest, I am not quite sure at the moment, why that is the case, but the '#' (detach) helps here.

 

As an addition: Note that your function does not really need to return anything, since it just returns the argument. As opposed to other places, even basic types are passed to functions as references. So, when calling the function above like this:

double d()
point p()
// .... initialize....
quadrantCorrection(p, d)

the variable d might have a different value after the call (except if the 1st Q case occurred). If you want to make sure that this does not happen, introduce a temporary variable (which, by the way, also takes away the need to use the detach):

function quadrantCorrection(FPoint startp, FDouble phi):FDouble
  double result(phi)
  if (AND(startp.getY()>=0,startp.getX()<0)) // 2th. Q.
    result= 180-result
  elseif (AND(startp.getY()>=0,startp.getX()>=0)) // 1st Q.
    result = result
  elseif (AND(startp.getY()<0,startp.getX()>=0)) // 4nd. Q.
    echo("Q4, phi " + result)
    result= 360-result
    echo("Q4, phiCorr " + result)
  elseif (AND(startp.getY()<0,startp.getX()<0)) // 3rd. Q.
    phi = 180+result
  endif
  return(result)
endfunction

I hope this helps.

 

Cheers,

Arne

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...