algorithmic modeling for Rhino
Hi,
I would to ask how to initialize class objects in an Array in C#.
For now I have a code:
private void RunScript(bool reset, object y, ref object A)
{
....bob functions
}
// <Custom additional code>
Bob[] b = new Bob[] {new Bob(1), new Bob(2), new Bob(3)};
class Bob{....
}
//But how to make something like this in a loop?
// <Custom additional code>
Bob[] b = new Bob[10];
for(int i = 0; Bob.Length; i++){
b[i] = new Bob(i);
}
Tags:
private void RunScript(bool reset, object y, ref object A)
{
_bob = new Bob[10];
for (int i = 1; i <= 10; i++)
_bob[i] = new Bob(i);
}
// <Custom additional code>
private Bob[] _bob;
Dear David,
Thank you for a reply.
The issue I have:
When I initialize _bob in RunScript and use timer, C# initialize the _bob each time script runs.
But I would like to initialize it once in order to increment some values of _bob (for instance x++ every 20 ms) It work works fine when I initialize Bob after Custom additional code. But I was wondering if it possible to initialize 100 _bobs wihout using this:
Bob[] _bob = new Bob[] {new Bob(1), new Bob(2), new Bob(3)... new Bob(100)};
I attached the .gh file.
I would really appreciate if you could help me.
Thank you,
Petras
I don't have your SimplexNoise.dll so I cannot run that script.
But if you want to give people the option to reset, then yes, having an input is the best and nearly only way.
Here's what I'd do (assuming a Reset input of type bool):
private void RunScript(bool Reset, ...)
{
if (Reset || _bob == null)
{
_bob = new Bob[10];
for (int i = 0; i < _bob.Length; i++)
_bob[i] = new Bob(i+1);
}
...
}
// Custom code.
private Bob[] _bob;
This way you ensure that _bob is never null, never contains null elements, still allow for a user reset, and that _bob will remain intact between calls to RunScript, as it is a class level variable.
Thank you very much.
I have other questions, that are not connected with this issue, but occured before:
1. Is it possible to get Noise(dobule a, double b, double c) values within c# component, instead of adding attached SimplexNoise.dll ?
2. How to permanently add .dll file so that it will be always visible within C# component? For now everytime I open new C# battery I have to load assembly again.
3. Is there a similar function to map(value, minA, maxA, minB, maxB) function within C#? I usually have to type remap values within different domains using this function:
public double map(double v1, double d1A, double d1B, double d2A, double d2B){
return d2A + (d2B - d2A) * ((v1 - d1A) / (d1B - d1A));
}
You can always write your own SimplexNoise, but since you already have a dll which does it that sounds like a waste of time and effort.
You cannot embed dlls in GH files, at least not through some mechanism already provided. It would technically be possible to convert the dll bytes into an array or string, hard-code that into the C# script and save that data as a dll file inside the script. It makes it harder to reference it though, you may have to resort of Reflection to invoke the types and methods. Or maybe not, if you handle the AssemblyResolve event, but now we're really getting into the central wastelands of Hackonia.
That mapping is quite common, but there doesn't appear to be anything inside .NET that does it. You can use Rhino.Geometry.Interval to do it, or just add that method to every assembly you write.
Thank you.
Incidentally Grasshopper2 has a large number of extension methods which provide useful functionality to all sort of types (doubles, integer, byte-arrays, strings, rhino geometry, you name it). I did add mapping functions both for normalised and non-normalised doubles, so at least whenever Grasshopper2.dll is a reference to your project, it'll make life a little bit easier, which doesn't help you right now of course...
/// <summary>
/// Map a normalised value to a new numeric domain.
/// </summary>
/// <param name="value">Value to map.</param>
/// <param name="targetMinimum">Start of target domain.</param>
/// <param name="targetMaximum">End of target domain.</param>
/// <returns>Mapped value.</returns>
public static double Map(this double value, double targetMinimum, double targetMaximum)
{
// Return the exact result in case of identity mapping.
if (targetMinimum.Equals(0.0) &&
targetMaximum.Equals(1.0))
return value;
// Return exact values for coincidence mapping.
if (value.Equals(0.0)) return targetMinimum;
if (value.Equals(1.0)) return targetMaximum;
return targetMinimum * (1.0 - value) + targetMaximum * value;
}
/// <summary>
/// Map a value from one numeric domain to another.
/// </summary>
/// <param name="value">Value to map.</param>
/// <param name="sourceMinimum">Start of source domain.</param>
/// <param name="sourceMaximum">End of source domain.</param>
/// <param name="targetMinimum">Start of target domain.</param>
/// <param name="targetMaximum">End of target domain.</param>
/// <returns>Mapped value.</returns>
public static double Map(this double value, double sourceMinimum, double sourceMaximum, double targetMinimum, double targetMaximum)
{
// Return the exact result in case of identity mapping.
if (sourceMinimum.Equals(targetMinimum) &&
sourceMaximum.Equals(targetMaximum))
return value;
// Normalise the value if it isn't already.
if (!sourceMinimum.Equals(0.0) || !sourceMaximum.Equals(1.0))
{
double d0 = value - sourceMinimum;
double d1 = sourceMaximum - sourceMinimum;
if (Math.Abs(d1) < 1e-128)
value = 0.5;
else
value = d0 / d1;
}
else
{
// Return exact values for coincidence mapping.
if (value.Equals(0.0)) return targetMinimum;
if (value.Equals(1.0)) return targetMaximum;
}
return targetMinimum * (1.0 - value) + targetMaximum * value;
}
Waiting for it!
When will Grasshopper 2.0 will be available?
(Probably everybody asks the same question.)
Is this the only way to add this code between conditional bool reset?
(When a boolean button is pressed _bob is initialized.)
private void RunScript(bool reset, object y, ref object A)
{
if(reset){
_bob = new Bob[10];
for (int i = 0; i < 10; i++){
_bob[i] = new Bob(i);
}
}
// <Custom additional code>
private Bob[] _bob;
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by