PDA

View Full Version : Sample use of PolygonObject (coffee)


johann.dugge
07-18-2006, 08:54 PM
Has anyone here got a little piece of coffee that I can (hardcode) coordinates of a triangle into and c4d will draw that triangle?

Cheers!

Johann

tcastudios
07-19-2006, 01:13 AM
Go here:
http://www.actual-fantasy.com/index_en.html

click the "puzzle" link and get Archimedes.

It does what you need and work fine even in R9.6.

Cheers
Lennart

johann.dugge
07-19-2006, 05:57 AM
Cheers Lennart! I think I should've made myself clearer. This is compiled coffee, which I cannot modify. I need that sample code as a reference / as a starting point to writing my own plugin.

Kuroyume0161
07-19-2006, 08:01 PM
This is just an example of generally how to create a Polygon object and add/set points and polygons. There is obviously more involved - such as making the type of plugin, adding main(). Full examples (links and text examples) and information are accessible through the COFFEE documentation.

// - Create a Polygon Object with 'pcount' polygons, 'vcount' points
var polyObject = new(PolygonObject);
var variableChanged = new(VariableChanged);
variableChanged->Init(pcount, vcount);
if (!polyObject->Message(MSG_POINTS_CHANGED, variableChanged))
{
TextDialog("Not Enough Memory to Complete Operation!",DLG_OK+DLG_ICONEXCLAMATION);
return;
}
polyObject->Message(MSG_UPDATE);

// - Set Polygon Object's point positions - valuex, valuey, valuez are
// example variables which should be the real XYZ numbers
// representing the points 3D location
var i;
var ptArray = polyObject->GetPoints();
for (i = 0; i < vcount; i++)
{
ptArray[i].x = valuex;
ptArray[i].y = valuey;
ptArray[i].z = valuez;
}
// Set Polygon point indices. indexA...indexD are example variables;
// you feed the indices - 'i' above - to create the triangle/quadrilateral
// from the points.
// Note that for triangles, polyArray[].d should be set to polyArray[].c
// *** Edited for posterity
var polyArray = polyObject->GetPolygons();
pcount *= 4;
for (i = 0; i < pcount; i+=4)
{
polyArray[i] = indexA;
polyArray[i+1] = indexB;
polyArray[i+2] = indexC;
polyArray[i+3] = indexD;
}
polyObject->SetPolygons(polyArray);

// - Insert into Document and notify
doc->InsertObject(polyObject, NULL, NULL);
GeEventAdd(DOCUMENT_CHANGED);

Hope that gives you a starting place.

Robert

JDP
07-19-2006, 08:04 PM
Ignore this post as Kuroyume0161 has already given you an answer, just beat me to it.

cheers,
JDP

johann.dugge
07-19-2006, 08:59 PM
Robert (and JDP of course ;) )
thank you so much, this is exactly what I had hoped for, fantastic!!

johann.dugge
07-20-2006, 12:05 PM
Robert, I've put your code into a coffee tag for now, to try it out. I modified it so that it should create a triangle.
However, the polyArray doesn't get initialised, "println(polyArray)" returns "nil".

Any idea why it doesn't work? Here's your code I modified:

main(doc,op)
{
// - Create a Polygon Object with 'pcount' polygons, 'vcount' points
var polyObject = new(PolygonObject);
var variableChanged = new(VariableChanged);
var pcount = 1;
var vcount = 4;
variableChanged->Init(pcount, vcount);
if (!polyObject->Message(MSG_POINTS_CHANGED, variableChanged))
{
TextDialog("Not Enough Memory to Complete Operation!",DLG_OK+DLG_ICONEXCLAMATION);
return;
}
polyObject->Message(MSG_UPDATE);

// - Set Polygon Object's point positions - valuex, valuey, valuez are
// example variables which should be the real XYZ numbers
// representing the points 3D location
var i;
var ptArray = polyObject->GetPoints();
println(ptArray); // Returns [Array-108EA4CC]


for (i = 0; i < vcount; i++)
{
ptArray[i].x = i*10;
ptArray[i].y = i*20;
ptArray[i].z = i*30;
}

println(ptArray[1].x); // Returns 10.000


// Set Polygon point indices. indexA...indexD are example variables;
// you feed the indices - 'i' above - to create the triangle/quadrilateral
// from the points.
// Note that for triangles, polyArray[].d should be set to polyArray[].c

var polyArray = polyObject->GetPolygons();
println(polyArray); // Returns nil


/*
for (i = 0; i < pcount; i++)
{
polyArray[i].a = 0;
polyArray[i].b = 1;
polyArray[i].c = 2;
polyArray[i].d = 3;
}
polyObject->SetPoints(ptArray);

// - Insert into Document and notify
doc->InsertObject(polyObject, NULL, NULL);
GeEventAdd(DOCUMENT_CHANGED);
*/
}

Kuroyume0161
07-20-2006, 04:32 PM
I didn't note that GetPolygons() in COFFEE returns an array formatted like a PolygonTag, but GetPolygon() returns a Polygon. It should still return an array, but maybe 1 polygon is too few (?). Replace this:

var polyArray = polyObject->GetPolygons();
println(polyArray); // Returns nil


/*
for (i = 0; i < pcount; i++)
{
polyArray[i].a = 0;
polyArray[i].b = 1;
polyArray[i].c = 2;
polyArray[i].d = 3;
}
polyObject->SetPoints(ptArray);


with this:

var poly;
poly.a = 0;
poly.b = 1;
poly.c = 2;
poly.d = 3;
if (!polyObject->SetPolygon(0, poly)) println("SetPolygon failed");

Robert

johann.dugge
07-20-2006, 10:14 PM
well, we're getting there...

I had to add this line to actually give polyObject the new points:

polyObject->SetPoints(ptArray);


the code you gave me I had to change like this to get it to work at all:

var poly = new(Polygon);


poly then actually stores the values I feed it, but the SetPolygon still returns FALSE.
The code then goes on to insert the polygon which has 4 points but no polys. The points I can select and create the polygon manually.
I'm thinking the new(Polygon) is still wrong.

Any idea? Thank you again btw, I really appreciate your effort!

Kuroyume0161
07-20-2006, 10:54 PM
Alright, let's do this right. This code has been tested. You'll need to supply your own icon.tif (if you want) and unique PluginID (or use one of the test range 1000001 to 1000010):

////////////////////////////////////////////////////////////////
// polyobject.cof
////////////////////////////////////////////////////////////////

// Included symbols
//include "c4d_symbols.h"

enum
{
// Unique ids from http://www.plugincafe.com
PLUGIN_MENU_ID = 1000001,

_DUMMY02_
};

var resource;
var plugin_path;
var plugin_icon;

// CLASS
class PolyObject_MenuPlugin : MenuPlugin
{
private:
public:
PolyObject_MenuPlugin();
GetID();
GetIcon();
GetName();
GetHelp();
Execute(doc);
}
PolyObject_MenuPlugin::PolyObject_MenuPlugin() { super(); }
PolyObject_MenuPlugin::GetID() { return PLUGIN_MENU_ID; }
PolyObject_MenuPlugin::GetName() { return "PolyObject"; }
PolyObject_MenuPlugin::GetHelp() { return "Help"; }
PolyObject_MenuPlugin::GetIcon() { return plugin_icon; }
PolyObject_MenuPlugin::Execute(doc)
{
var pcount = 1;
var vcount = 4;
// - Create a Polygon Object with 'pcount' polygons, 'vcount' points
var polyObject = new(PolygonObject);

var variableChanged = new(VariableChanged);
variableChanged->Init(0, vcount);
if (!polyObject->Message(MSG_POINTS_CHANGED, variableChanged))
{
TextDialog("Not Enough Memory to Complete Operation!",DLG_OK+DLG_ICONEXCLAMATION);
return FALSE;
}
polyObject->Message(MSG_UPDATE);

// - Set Polygon Object's point positions - valuex, valuey, valuez are
// example variables which should be the real XYZ numbers
// representing the points 3D location
var i;
var ptArray = polyObject->GetPoints();
for (i = 0; i < vcount; i++)
{
ptArray[i].x = i * 10;
ptArray[i].y = i * 20;
ptArray[i].z = i * 30;
}
polyObject->SetPoints(ptArray);

variableChanged->Init(0, pcount);
if (!polyObject->Message(MSG_POLYGONS_CHANGED, variableChanged))
{
TextDialog("Not Enough Memory to Complete Operation!",DLG_OK+DLG_ICONEXCLAMATION);
return FALSE;
}
polyObject->Message(MSG_UPDATE);

// Set Polygon point indices. indexA...indexD are example variables;
// you feed the indices - 'i' above - to create the triangle/quadrilateral
// from the points.
// Note that for triangles, polyArray[].d should be set to polyArray[].c
// *** Edited for posterity

var poly = polyObject->GetPolygon(0);
poly->a = 0;
poly->b = 1;
poly->c = 2;
poly->d = 3;
if (!polyObject->SetPolygon(0, poly)) println("SetPolygon failed");

// - Insert into Document and notify
doc->InsertObject(polyObject, NULL, NULL);
GeEventAdd(DOCUMENT_CHANGED);
return TRUE;
}

// Start of plugin
main()
{
// Load Resource
if (!(plugin_path = GeGetRootFilename())) println("Unable to find plugin!");
plugin_path->RemoveLast();

// Load Icons
var icon_file = plugin_path->GetClone();
icon_file->AddLast("pluginicon.tif");
plugin_icon = new(BaseBitmap,1,1);
plugin_icon->Load(icon_file);

// Register Plugins
Register(PolyObject_MenuPlugin);
}


Sorry about the bad code previously.

Robert

johann.dugge
07-21-2006, 11:26 PM
Thank you Robert so much! This is excellent and it works fine. I think other people wanting to get into plug-in writing will enjoy this very much. I'll work through the code tomorrow and try to understand it.
Again, the time and dedication you put into this, unbelievable! :applause:

CGTalk Moderation
07-21-2006, 11:26 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.