(defglobal ?*ROOTDIMENSION*   = 0)
(defglobal ?*POLYFLOW*   = (str-cat (Getenv POLYFLOW) "/bin/polyflow"))

;=================================================================================
;

(deffunction CallBackNew (?a)
       (bind ?W (gensym*))
       (make-instance ?W of NEWW)
       (send (instance-address ?W) Show)
)

;  This helps for the direct access to creation of new items
;


(deffunction ShowRootMesh ()


      (bind $?L (find-all-instances ((?I READ_IN_FILE)) TRUE))

      (bind ?CoordIns (instance-address (nth$ 1 $?L)))

      (bind ?*toposhow* (topo-create-show))

      (pd-load-mesh ?*toposhow* (send ?CoordIns get-P4ObjId))

      (topo-set-background ?*toposhow* .7 .7 .7)
      (topo-adjust-light ?*toposhow*)

      (bind $?L (find-instance ((?I GEOM)) (eq ?I:GeomAttribute Root)))
      (bind ?*ROOTDIMENSION* (send (instance-address (nth$ 1 $?L)) get-GeomDimension))

      (bind ?Entity (MakeEntity 1))
      (bind ?ShowRoot  (pd-load-entity ?*toposhow* ?Entity))
      (DeleteEntity ?Entity)


      (topo-set-color ?ShowRoot 0. 0. 0.)
      (topo-set-curve ?ShowRoot)
      (topo-show ?*toposhow* ?ShowRoot)
      (bind ?*ShowRootGlobal* ?ShowRoot)

     (bind ?x (- (topo-get-x-max ?*toposhow*) (topo-get-x-min ?*toposhow*)) )
     (bind ?y (- (topo-get-y-max ?*toposhow*) (topo-get-y-min ?*toposhow*)) )
     (bind ?z (- (topo-get-z-max ?*toposhow*) (topo-get-z-min ?*toposhow*)) )

     (bind ?len (sqrt (+ (** ?x 2) (** ?y 2) (** ?z 2))  ))

     (topo-hide-axes ?*toposhow*)
     (topo-set-axes ?*toposhow* ?x ?y ?z (* ?len 10.) )

     (topo-reset-camera ?*toposhow*)
     (topo-run-show ?*toposhow*)
     (topo-render-show ?*toposhow*)
)



(deffunction CallBackReadMesh (?a)

  (bind $?L (find-instance ((?I ENVIRONMENT)) TRUE))
  (if (eq (length$ $?L) 1) then

    (bind ?file (file-selector (Translate ReadMesh)
                                 ""
                               "msh"
                               "msh"
                               "*msh*"
                 )
     )
     (send (instance-address (nth$ 1 $?L)) put-MeshName ?file)
     (if (eq (file-exists ?file) 1) then
      (assert (ReadMesh ?file 1))
      (run)
      (do-for-all-instances ((?I READ_IN_FILE)) TRUE (send ?I Read ?file))

      (ShowRootMesh)

     )
  )
)

(deffunction CallBackLoad (?a)
  (bind ?file (file-selector (Translate Load)
                               ""
                               "myrun"
               )
  )
  (if (eq (file-exists ?file) 1) then
    (bind ?f (assert   (Convert_To_Instances_Names))) 
    (run)
    (retract ?f)
    (restore-instances ?file)
    (bind ?f (assert   (Convert_To_Instances_Addresses))) 
    (run)
    (retract ?f)
    (bind ?f (assert   (Convert_Geom_Extensions))) 
    (run)
    (retract ?f)
    (bind ?Sid (send [Environment] get-Context))
    (bind ?f (assert (Context ?Sid)))
    (run)
    (retract ?f)

    (Load_Into_Store (Give-File-Instance-Name ?file [TOPO1]) (instance-address [TOPO1]))
    (send [TOPO1] put-P4ObjId 1)
    (bind ?f (assert (Reload_Mesh_Extensions)))
    (run)
    (retract ?f)

    (bind $?L (find-all-instances ((?I MODEL)) (or (eq ?I:ModelStatus Allocated)(eq ?I:ModelStatus Done) )))
    (loop-for-count (?C 1 (length$ $?L)) do
       (bind ?Ins (instance-address (nth$ ?C $?L)))
       (send ?Ins Allocate)
       (Load_Into_Store (Give-File-Instance-Name ?file ?Ins) ?Ins)
    )

    (ShowRootMesh)
  )
)

(deffunction SaveAsFile (?file)

  (if (eq (file-exists ?file) 1) then
    (Rm ?file)
    (CShell (str-cat "rm -r " ?file ".ext"))
  )
  (save-instances ?file)
  (mkdir (str-cat ?file ".ext"))
  (bind $?L (find-all-instances ((?I GEOM)) (eq ?I:P4ObjCreationFact (create$ Root))))
  (loop-for-count (?C 1 (length$ $?L)) do
     (bind ?Ins (nth$ ?C $?L))
     (bind ?FName (Give-File-Instance-Name ?file ?Ins))
     (if (eq (file-exists ?FName) 0) then
       (Save_From_Store ?FName (instance-address ?Ins))
     )
  )
  (bind $?L (find-all-instances ((?I MODEL)) (> ?I:P4ObjId 0)))
  (loop-for-count (?C 1 (length$ $?L)) do
     (bind ?Ins (nth$ ?C $?L))
     (Save_From_Store (Give-File-Instance-Name ?file ?Ins) (instance-address ?Ins))
  )
)

(deffunction CallBackSave (?a)
  (bind ?file (str-cat (send [Environment] get-Identificator)))
  (SaveAsFile ?file)
)

(deffunction CallBackSaveAs (?a)
  (bind ?file (file-selector (Translate Load)
                               ""
                               "myrun"
               )
  )
  (SaveAsFile ?file)
)


(deffunction CallBackExit (?a)
  (exit)
)

(deffunction CallBackInfo (?a)
)

(deffunction CallBackReorder (?a)
)

(deffunction CallBackModels (?a)
  (bind ?W (gensym*))
  (make-instance ?W of BASICW)
  (send (instance-address ?W) Show)
)

(deffunction CallBackMatDat(?a)
  (bind ?W (gensym*))
  (bind $?LIns (find-all-instances ((?I INPUT_MATERIAL_DATA)) TRUE))
  (bind ?W (gensym*))
  (make-instance ?W of MATDATW (XSize 700)(Class INPUT_MATERIAL_DATA)(TopModels ?*ModelsMatDat*))
  (send (instance-address ?W) Show)
)

(deffunction CallBackBConditions (?a)
  (bind ?W (gensym*))
  (bind $?LIns (find-all-instances ((?I INPUT_SOLVER_CONSTRAINT)) TRUE))
  (bind ?W (gensym*))
  (make-instance ?W of BCONDW (XSize 700)(Class INPUT_SOLVER_CONSTRAINT)(TopModels ?*ModelsBCond*))
  (send (instance-address ?W) Show)
)

(deffunction CallBackRestraints (?a)
  (bind $?LIns (find-all-instances ((?I ELLIPTIC)) (eq (send (send ?I get-ModelGeom) get-GeomDimension)?*ROOTDIMENSION*)))
  (bind ?W (gensym*))
  (make-instance ?W of RESTRAINTW (XSize 700)(Models $?LIns)(CustomFathers TRUE)(ShowAllSubGeoms TRUE))
  (send (instance-address ?W) Show)
)

(deffunction CallBackInitialize (?a)
   (do-for-all-instances ((?I SOLVER)) TRUE (send ?I delete))
   (bind $?L (find-all-instances ((?I MODEL)) TRUE))
   (loop-for-count (?C 1 (length$ $?L)) do
      (bind ?Ins (instance-address (nth$ ?C $?L)))
      (send ?Ins put-ModelStatus Building)
      (send ?Ins DeAllocate)
   )
)

(deffunction CallBackGo (?a)

  (do-for-all-instances ((?I SOLVER)) TRUE (send ?I delete))
  (bind ?file (str-cat (send [Environment] get-Identificator) ".pfl"))
  (SaveAsFile ?file)
  (bind ?Command (str-cat ?*POLYFLOW* " -4 -L " ?file))
  (CShell ?Command)

  (do-for-all-instances ((?I MODEL)) TRUE (send ?I DeAllocate))

  (bind ?f (assert   (Convert_To_Instances_Names))) 
  (run)
  (retract ?f)
  (restore-instances ?file)
  (bind ?f (assert   (Convert_To_Instances_Addresses))) 
  (run)
  (retract ?f)
  (bind ?Sid (send [Environment] get-Context))
  (bind ?f (assert (Context ?Sid)))
  (run)
  (retract ?f)

;  Ceci sera necessaire si le solveur peut changer la topologie des reseau.
;  Pour le moment, ce n'est pas le cas!
;    (Load_Into_Store (Give-File-Instance-Name ?file [TOPO1]) (instance-address [TOPO1]))
;    (send [TOPO1] put-P4ObjId 1)
;    (bind ?f (assert (Reload_Mesh_Extensions)))
;    (run)
;    (retract ?f)


  (bind $?L (find-all-instances ((?I MODEL)) TRUE ))
  (loop-for-count (?C 1 (length$  $?L)) do
     (bind ?Ins (instance-address (nth$ ?C $?L)))
     (send ?Ins Allocate)
     (Load_Into_Store (Give-File-Instance-Name ?file ?Ins) (instance-address ?Ins))
  )

;  Update the graphic, just in case the topology or the coordinates
;  have been modified!
  (bind ?f (assert (DeleteAllEntityId)))
  (run)
  (retract ?f)

  (topo-delete-show ?*toposhow*)
  (ShowRootMesh)
)

(deffunction CallBackRenameGeom (?a)
    (bind ?W (gensym*))
    (make-instance ?W of RENAMEGEOMW)
    (send (instance-address ?W) Show)
)

(deffunction CallBackCreateZ0D (?a)
  (bind $?L (find-all-instances ((?I GEOM)) TRUE))
  (bind $?GeomList (create$))
  (loop-for-count (?C 1 (length$ $?L)) do
     (bind $?GeomList (instance-address (nth$ ?C $?L)) $?GeomList)
  )

    (bind ?W (gensym*))
    (make-instance ?W of CREATEGEOMW (GeomList $?GeomList))
    (send (instance-address ?W) Show)
)

(deffunction CallBackDeleteZ (?a)
)

(deffunction CallBackFlPost (?a)
  (FLPOST p4)
;;  (bind ?Command (str-cat "flpost " ?*ROOTDIMENSION* "d"))
  (bind ?Command (str-cat "flpost 3d"))
  (CShell ?Command)
)

(deffunction CallBackDebug (?a)
  (bind ?f (assert (Context Debug)))
  (run)
  (retract ?f)
  (assert   (DebuggingOn))
  (run)