sfgdemo.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # demo program to run sfg layout using python
  2. # first run make swigpython to create the _sfg.so file
  3. import sfg
  4. # get the version number of sfg.c
  5. version=sfg.sfg_version()
  6. print("sfg.c version number is " + str(version));
  7. # all routines return status <0 at error
  8. # and the error codes are in sfg.h found
  9. # first do init
  10. status=sfg.sfg_init();
  11. # a parser must first add all nodes then edges
  12. # node and edge numbers must start with 1 and increase by 1
  13. # add node 1,2 and 3 with size (20,10)
  14. status=sfg.sfg_addnode(1,20,10);
  15. status=sfg.sfg_addnode(2,20,10);
  16. status=sfg.sfg_addnode(3,20,10);
  17. # add edge 1 and 2 from node 1 to 2 and 1 to 3
  18. # the last two numbers are optional size of edgelabel or 0 if no edgelabel
  19. status=sfg.sfg_addedge(1,1,2,0,0);
  20. status=sfg.sfg_addedge(2,1,3,0,0);
  21. # run sugiyama barycenter layout
  22. status=sfg.sfg_layout();
  23. # get lowest and higest node number in use after layout */
  24. nodemin=sfg.sfg_nodemin();
  25. nodemax=sfg.sfg_nodemax();
  26. print("node numbers are in range " + str(nodemin) + " ... " + str(nodemax));
  27. # sfgdmo.c is using a callback routine in c to get the data
  28. # this is manaul getting the node and edge data
  29. for nn in range (nodemin, nodemax+1):
  30. xpos=sfg.sfg_nodexpos(nn);
  31. ypos=sfg.sfg_nodeypos(nn);
  32. print("node " + str(nn) + " is at position (" + str(xpos) +"," + str(ypos) + ")");
  33. # get lowest and higest edge number in use after layout */
  34. edgemin=sfg.sfg_edgemin();
  35. edgemax=sfg.sfg_edgemax();
  36. print("edge numbers are in range " + str(edgemin) + " ... " + str(edgemax));
  37. for ne in range (edgemin, edgemax+1):
  38. fn=sfg.sfg_edgefrom(ne);
  39. tn=sfg.sfg_edgeto(ne);
  40. print("edge "+ str(ne) + " is between node " + str(fn) + " and node " + str(tn));
  41. # sfgdemo.c has example how to create svg image date
  42. # gml4gtk has in main.c example how to using cairo lib
  43. print("and here is the svg image data");
  44. width=sfg.sfg_maxx() + 1;
  45. height=sfg.sfg_maxy() + 5;
  46. print("<svg width=\"" + str(width) + "\" height=\"" + str(height) + "\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:svg=\"http://www.w3.org/2000/svg\">");
  47. print(" <g>");
  48. print(" <title>sfgdemo.py</title>");
  49. print(" <style>");
  50. print(" .small { font: italic 15px sans-serif; }");
  51. print(" </style>");
  52. # get lowest and higest node number in use after layout */
  53. nodemin=sfg.sfg_nodemin();
  54. nodemax=sfg.sfg_nodemax();
  55. for nn in range (nodemin, nodemax+1):
  56. tx=sfg.sfg_nodexsize(nn);
  57. ty=sfg.sfg_nodeysize(nn);
  58. xpos=sfg.sfg_nodexpos(nn);
  59. ypos=sfg.sfg_nodeypos(nn);
  60. print(" <rect id=\"node_" + str(nn) + "\" height=\"" + str(ty) + "\" width=\"" + str(tx) + "\" y=\"" + str(ypos) + "\" x=\"" + str(xpos) + "\" stroke-linecap=\"null\" stroke-linejoin=\"null\" stroke-dasharray=\"null\" stroke=\"#000000\" fill=\"#7fff00\"/>");
  61. # get lowest and higest edge number in use after layout */
  62. edgemin=sfg.sfg_edgemin();
  63. edgemax=sfg.sfg_edgemax();
  64. for ne in range (edgemin, edgemax+1):
  65. rev=sfg.sfg_edgerev(ne);
  66. scolor = "#000000";
  67. if (rev == 1):
  68. scolor = "#0f0fff";
  69. fn=sfg.sfg_edgefrom(ne);
  70. tn=sfg.sfg_edgeto(ne);
  71. typef=sfg.sfg_nodetype(fn);
  72. typet=sfg.sfg_nodetype(tn);
  73. fx=sfg.sfg_nodexpos(fn);
  74. fy=sfg.sfg_nodeypos(fn);
  75. tx=sfg.sfg_nodexpos(tn);
  76. ty=sfg.sfg_nodeypos(tn);
  77. szxf=sfg.sfg_nodexsize(fn);
  78. szyf=sfg.sfg_nodeysize(fn);
  79. szxt=sfg.sfg_nodexsize(tn);
  80. szyt=sfg.sfg_nodeysize(tn);
  81. if (typef == 1):
  82. fx = fx + (szxf / 2);
  83. fy = fy + szyf;
  84. if (typet == 1):
  85. tx = tx + (szxt / 2);
  86. print(" <line id=\"edge_" + str(ne) + "\" y2=\"" + str(ty) + "\" x2=\"" + str(tx) + "\" y1=\"" + str(fy) + "\" x1=\"" + str(fx) + "\" stroke-linecap=\"null\" stroke-linejoin=\"null\" stroke-dasharray=\"null\" stroke-width=\"2\" stroke=\"" + scolor + "\" fill=\"none\"/>");
  87. print(" </g>");
  88. print("</svg>");
  89. # final do de-init and all memory is free()'d
  90. status=sfg.sfg_deinit();