OSDN Git Service

The native format (mbsf) now preserves the display conditions, such as scale, orienta...
authortoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Sun, 16 May 2010 03:09:36 +0000 (03:09 +0000)
committertoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Sun, 16 May 2010 03:09:36 +0000 (03:09 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/molby/trunk@56 a2be9bc6-48de-4e38-9406-05402d4bc13c

Makefile
MolLib/MainView.c
MolLib/MainView.h
MolLib/Molecule.c
MolLib/Ruby_bind/ruby_bind.c
MolLib/Trackball.c
MolLib/Trackball.h
Scripts/loadsave.rb
wxSources/MyDocManager.cpp
wxSources/MyDocument.cpp
xcode-build/Molby.xcodeproj/project.pbxproj

index a55ccb0..453e9ea 100755 (executable)
--- a/Makefile
+++ b/Makefile
@@ -21,7 +21,7 @@ ifeq ($(TARGET_PLATFORM),MSW)
 endif
 
 WXLIB_LIST = core,base,gl,adv
-OBJECTS = ConsoleFrame.o GlobalParameterFrame.o GlobalParameterFilesFrame.o MoleculeView.o MyApp.o MyCommand.o MyDocument.o MyGLCanvas.o MySlider.o MyClipboardData.o ProgressFrame.o MyListCtrl.o MyDocManager.o wxKillAddition.o RubyDialogFrame.o MyVersion.o MyThread.o MolLib.a Ruby_bind.a
+OBJECTS = ConsoleFrame.o GlobalParameterFrame.o GlobalParameterFilesFrame.o MoleculeView.o MyApp.o MyCommand.o MyDocument.o MyGLCanvas.o MySlider.o MyClipboardData.o ProgressFrame.o MyListCtrl.o MyDocManager.o wxKillAddition.o docview.o RubyDialogFrame.o MyVersion.o MyThread.o MolLib.a Ruby_bind.a
 RUBY_EXTLIB = scanf.rb
 
 ifeq ($(TARGET_PLATFORM),MAC)
index d4b1326..9ed16b1 100755 (executable)
@@ -99,7 +99,7 @@ MainView_setMolecule(MainView *mview, struct Molecule *mol)
                MoleculeRetain(mol);
                mol->mview = mview;  /*  No retain  */
                MainViewCallback_moleculeReplaced(mview, mol);
-               MainView_resizeToFit(mview);
+       /*      MainView_resizeToFit(mview); */
                MoleculeCallback_notifyModification(mol, 0);
        /*      MainViewCallback_setNeedsDisplay(mview, 1); */
        }
index 8d143fb..ff0b5ce 100755 (executable)
@@ -99,6 +99,7 @@ typedef struct MainView {
        float bondRadius; /* in angstrom */
        float probabilityScale;
        float dimension;
+       
        Byte showUnitCell;
        Byte showPeriodicBox;
        Byte showExpandedAtoms;
index e71531d..734f4bd 100755 (executable)
@@ -329,19 +329,30 @@ void
 MoleculeExchange(Molecule *mp1, Molecule *mp2)
 {
        Molecule mp_temp;
+       struct MainView *mview1, *mview2;
+       struct MDArena *arena1, *arena2;
+       /*  mview and arena must be kept as they are  */
+       mview1 = mp1->mview;
+       mview2 = mp2->mview;
+       arena1 = mp1->arena;
+       arena2 = mp2->arena;
        /*  'natoms' is the first member to be copied  */
        int ofs = offsetof(Molecule, natoms);
        memmove((char *)(&mp_temp) + ofs, (char *)mp1 + ofs, sizeof(Molecule) - ofs);
        memmove((char *)mp1 + ofs, (char *)mp2 + ofs, sizeof(Molecule) - ofs);
        memmove((char *)mp2 + ofs, (char *)(&mp_temp) + ofs, sizeof(Molecule) - ofs);
-       if (mp1->arena != NULL && mp1->arena->mol == mp2)
+       mp1->arena = arena1;
+       mp2->arena = arena2;
+       mp1->mview = mview1;
+       mp2->mview = mview2;
+/*     if (mp1->arena != NULL && mp1->arena->mol == mp2)
                mp1->arena->mol = mp1;
        if (mp1->arena != NULL && mp1->arena->xmol == mp2)
                mp1->arena->xmol = mp1;
        if (mp2->arena != NULL && mp2->arena->mol == mp1)
                mp2->arena->mol = mp2;
        if (mp2->arena != NULL && mp2->arena->xmol == mp1)
-               mp2->arena->xmol = mp2;
+               mp2->arena->xmol = mp2; */
 }
 
 #pragma mark ====== Mutex ======
@@ -470,13 +481,15 @@ int
 MoleculeLoadMbsfFile(Molecule *mol, const char *fname, char *errbuf, int errbufsize)
 {
        FILE *fp;
-       Molecule *mp;
        char buf[1024];
        int i, j, k, n, err, fn, nframes;
        int lineNumber;
+       Molecule *mp;
        int ibuf[12];
        Int iibuf[4];
        double dbuf[12];
+       int mview_ibuf[16];
+       float mview_fbuf[8];
        char cbuf[12][6];
        const char **pp;
        char *bufp, *valp, *comp;
@@ -484,18 +497,27 @@ MoleculeLoadMbsfFile(Molecule *mol, const char *fname, char *errbuf, int errbufs
        Double *dp;
        Vector v;
        Atom *ap;
+       const int kUndefined = -10000000;
        err = 0;
        if (errbuf == NULL) {
                errbuf = buf;
                errbufsize = 1024;
        }
        errbuf[0] = 0;
-       mp = MoleculeNew();
+       if (mol->natoms != 0) {
+               snprintf(errbuf, errbufsize, "The molecule must be empty");
+               return 1;
+       }
        fp = fopen(fname, "rb");
        if (fp == NULL) {
                snprintf(errbuf, errbufsize, "Cannot open file");
                return 1;
        }
+       mp = MoleculeNew();  /*  Temporary molecule  */
+       for (i = 0; i < 8; i++)
+               mview_fbuf[i] = kUndefined;
+       for (i = 0; i < 16; i++)
+               mview_ibuf[i] = kUndefined;
        /*      flockfile(fp); */
        lineNumber = 0;
        fn = 0;
@@ -965,6 +987,57 @@ MoleculeLoadMbsfFile(Molecule *mol, const char *fname, char *errbuf, int errbufs
                                free(bufp);
                        }
                        continue;
+               } else if (strstr(buf, "!:trackball") == buf) {
+                       i = 0;
+                       while (ReadLine(buf, sizeof buf, fp, &lineNumber) > 0) {
+                               if (buf[0] == '!')
+                                       continue;
+                               if (buf[0] == '\n')
+                                       break;
+                               /* scale; trx try trz; theta_deg x y z */
+                               if ((i == 0 && sscanf(buf, "%f", &mview_fbuf[0]) < 1)
+                                       || (i == 1 && sscanf(buf, "%f %f %f",
+                                                                                &mview_fbuf[1], &mview_fbuf[2], &mview_fbuf[3]) < 3)
+                                       || (i == 2 && sscanf(buf, "%f %f %f %f",
+                                                                                &mview_fbuf[4], &mview_fbuf[5], &mview_fbuf[6], &mview_fbuf[7]) < 4)) {
+                                       snprintf(errbuf, errbufsize, "line %d: bad trackball format", lineNumber);
+                                       goto exit;
+                               }
+                               i++;
+                       }
+                       continue;
+               } else if (strstr(buf, "!:view") == buf) {
+                       while (ReadLine(buf, sizeof buf, fp, &lineNumber) > 0) {
+                               if (buf[0] == '!')
+                                       continue;
+                               if (buf[0] == '\n')
+                                       break;
+                               bufp = buf;
+                               comp = strsep(&bufp, " \t");
+                               if (bufp != NULL) {
+                                       while (*bufp == ' ' || *bufp == '\t')
+                                               bufp++;
+                                       valp = strsep(&bufp, "\n");
+                               } else valp = NULL;
+                               /*  In the following, the redundant "!= NULL" is to suppress suprious warning  */
+                               if ((strcmp(comp, "show_unit_cell") == 0 && (i = 1))
+                                       || (strcmp(comp, "show_periodic_box") == 0 && (i = 2))
+                                       || (strcmp(comp, "show_expanded_atoms") == 0 && (i = 3))
+                                       || (strcmp(comp, "show_ellipsoids") == 0 && (i = 4))
+                                       || (strcmp(comp, "show_hydrogens") == 0 && (i = 5))
+                                       || (strcmp(comp, "show_dummy_atoms") == 0 && (i = 6))
+                                       || (strcmp(comp, "show_rotation_center") == 0 && (i = 7))
+                                       || (strcmp(comp, "show_graphite_flag") == 0 && (i = 8))
+                                       || (strcmp(comp, "show_periodic_image_flag") == 0 && (i = 9))
+                                       || (strcmp(comp, "show_graphite") == 0 && (i = 10))) {
+                                       mview_ibuf[i - 1] = atoi(valp);
+                               } else if (strcmp(comp, "show_periodic_image") == 0) {
+                                       sscanf(valp, "%d %d %d %d %d %d",
+                                                  &mview_ibuf[10], &mview_ibuf[11], &mview_ibuf[12],
+                                                  &mview_ibuf[13], &mview_ibuf[14], &mview_ibuf[15]);
+                               }
+                       }
+                       continue;
                }
                /*  Unknown sections are silently ignored  */
        }
@@ -976,14 +1049,47 @@ MoleculeLoadMbsfFile(Molecule *mol, const char *fname, char *errbuf, int errbufs
 exit:
        fclose(fp);
        if (errbuf[0] != 0) {
-               free(mp);
+               MoleculeRelease(mp);
                return -1;
        } else {
                MoleculeExchange(mp, mol);
                MoleculeRelease(mp);
+               if (mol->mview != NULL) {
+                       if (mview_ibuf[0] != kUndefined)
+                               mol->mview->showUnitCell = mview_ibuf[0];
+                       if (mview_ibuf[1] != kUndefined)
+                               mol->mview->showPeriodicBox = mview_ibuf[1];
+                       if (mview_ibuf[2] != kUndefined)
+                               mol->mview->showExpandedAtoms = mview_ibuf[2];
+                       if (mview_ibuf[3] != kUndefined)
+                               mol->mview->showEllipsoids = mview_ibuf[3];
+                       if (mview_ibuf[4] != kUndefined)
+                               mol->mview->showHydrogens = mview_ibuf[4];
+                       if (mview_ibuf[5] != kUndefined)
+                               mol->mview->showDummyAtoms = mview_ibuf[5];
+                       if (mview_ibuf[6] != kUndefined)
+                               mol->mview->showRotationCenter = mview_ibuf[6];
+                       if (mview_ibuf[7] != kUndefined)
+                               mol->mview->showGraphiteFlag = mview_ibuf[7];
+                       if (mview_ibuf[8] != kUndefined)
+                               mol->mview->showPeriodicImageFlag = mview_ibuf[8];
+                       if (mview_ibuf[9] != kUndefined)
+                               mol->mview->showGraphite = mview_ibuf[9];
+                       for (i = 0; i < 6; i++) {
+                               if (mview_ibuf[10 + i] != kUndefined)
+                                       mol->mview->showPeriodicImage[i] = mview_ibuf[10 + i];
+                       }
+                       if (mol->mview->track != NULL) {
+                               if (mview_fbuf[0] != kUndefined)
+                                       TrackballSetScale(mol->mview->track, mview_fbuf[0]);
+                               if (mview_fbuf[1] != kUndefined)
+                                       TrackballSetTranslate(mol->mview->track, mview_fbuf + 1);
+                               if (mview_fbuf[4] != kUndefined)
+                                       TrackballSetRotate(mol->mview->track, mview_fbuf + 4);
+                       }
+               }
        }
        return 0;
-       
 }
 
 int
@@ -2981,7 +3087,37 @@ MoleculeWriteToMbsfFile(Molecule *mp, const char *fname, char *errbuf, int errbu
                        fprintf(fp, "%d %.8f %.8f %.8f\n", i, ap->f.x, ap->f.y, ap->f.z);
                }
                fprintf(fp, "\n");
-               
+       }
+       
+       if (mp->mview != NULL) {
+               float f[4];
+               if (mp->mview->track != NULL) {
+                       fprintf(fp, "!:trackball\n");
+                       fprintf(fp, "! scale; trx try trz; theta_deg x y z\n");
+                       f[0] = TrackballGetScale(mp->mview->track);
+                       fprintf(fp, "%f\n", f[0]);
+                       TrackballGetTranslate(mp->mview->track, f);
+                       fprintf(fp, "%f %f %f\n", f[0], f[1], f[2]);
+                       TrackballGetRotate(mp->mview->track, f);
+                       fprintf(fp, "%f %f %f %f\n", f[0], f[1], f[2], f[3]);
+                       fprintf(fp, "\n");
+               }
+               fprintf(fp, "!:view\n");
+               fprintf(fp, "show_unit_cell %d\n", mp->mview->showUnitCell);
+               fprintf(fp, "show_periodic_box %d\n", mp->mview->showPeriodicBox);
+               fprintf(fp, "show_expanded_atoms %d\n", mp->mview->showExpandedAtoms);
+               fprintf(fp, "show_ellipsoids %d\n", mp->mview->showEllipsoids);
+               fprintf(fp, "show_hydrogens %d\n", mp->mview->showHydrogens);
+               fprintf(fp, "show_dummy_atoms %d\n", mp->mview->showDummyAtoms);
+               fprintf(fp, "show_rotation_center %d\n", mp->mview->showRotationCenter);
+               fprintf(fp, "show_graphite_flag %d\n", mp->mview->showGraphiteFlag);
+               fprintf(fp, "show_graphite %d\n", mp->mview->showGraphite);
+               fprintf(fp, "show_periodic_image_flag %d\n", mp->mview->showPeriodicImageFlag);
+               fprintf(fp, "show_periodic_image %d %d %d %d %d %d\n",
+                               mp->mview->showPeriodicImage[0], mp->mview->showPeriodicImage[1],
+                               mp->mview->showPeriodicImage[2], mp->mview->showPeriodicImage[3],
+                               mp->mview->showPeriodicImage[4], mp->mview->showPeriodicImage[5]);
+               fprintf(fp, "\n");
        }
 
        fclose(fp);
index b067cc6..cba055e 100644 (file)
@@ -5268,10 +5268,7 @@ s_Molecule_Extract(int argc, VALUE *argv, VALUE self)
        if (MoleculeExtract(mol1, &mol2, ig, (dummy_flag != Qnil && dummy_flag != Qfalse)) != 0) {
                retval = Qnil;
        } else {
-               retval = s_Molecule_Alloc(rb_cMolecule);
-               Data_Get_Struct(retval, Molecule, mol1);
-               MoleculeExchange(mol1, mol2);
-               MoleculeRelease(mol2);
+               retval = ValueFromMolecule(mol2);
        }
        IntGroupRelease(ig);
        return retval;
index 4a0c79b..ca08f55 100644 (file)
@@ -117,6 +117,12 @@ TrackballRelease(Trackball *track)
        }
 }
 
+float
+TrackballGetScale(const Trackball *track)
+{
+       NULL_CHECK(track, "TrackballGetScale"); 
+    return track->tempScale + track->scale;
+}
 
 /*  Get the current rotation (in GL format)  */
 //- (void)getRotate:(float *)a
@@ -190,6 +196,12 @@ TrackballGetPerspective(const Trackball *track, float *a)
     }
 }
 
+int
+TrackballGetModifyCount(Trackball *track)
+{
+       return (track != NULL ? track->modifyCount : 0);
+}
+
 void
 TrackballReset(Trackball *track)
 {
@@ -201,6 +213,7 @@ TrackballReset(Trackball *track)
     track->quat[0] = track->tempQuat[0] = 1.0;
        track->tempScale = 0;
        track->scale = 0;
+       track->modifyCount++;
 }
 
 void
@@ -209,6 +222,22 @@ TrackballSetScale(Trackball *track, float scale)
        NULL_CHECK(track, "TrackballSetScale");
        track->tempScale = 0;
        track->scale = scale;
+       track->modifyCount++;
+}
+
+void
+TrackballSetRotate(Trackball *track, const float *a)
+{
+       float k; 
+       NULL_CHECK(track, "TrackballSetRotate");
+       track->tempQuat[0] = 1.0;
+       track->tempQuat[1] = track->tempQuat[2] = track->tempQuat[3] = 0.0;
+       track->quat[0] = cos(a[0] * 0.5 * kDeg2Rad);
+       k = sin(a[0] * 0.5 * kDeg2Rad);
+       track->quat[1] = k * a[1];
+       track->quat[2] = k * a[2];
+       track->quat[3] = k * a[3];
+       track->modifyCount++;
 }
 
 void
@@ -219,6 +248,7 @@ TrackballSetTranslate(Trackball *track, const float *a)
        track->trans[0] = a[0];
        track->trans[1] = a[1];
        track->trans[2] = a[2];
+       track->modifyCount++;
 }
 
 #pragma mark ====== Mouse operations ======
index 176b3d1..adbf01e 100644 (file)
@@ -62,20 +62,26 @@ typedef struct Trackball {
     float tempTrans[3];        /*  Temporary translation  */
     float scale;               /*  Scale  */
     float tempScale;   /*  Temporary scale */
+       
+       int   modifyCount;
 } Trackball;
 
 Trackball *TrackballNew(void);
 void TrackballRetain(Trackball *track);
 void TrackballRelease(Trackball *track);
 
+float TrackballGetScale(const Trackball *track);
 void TrackballGetRotate(const Trackball *track, float *a);
 void TrackballGetTranslate(const Trackball *track, float *a);
 void TrackballGetPerspective(const Trackball *track, float *a);
 
 void TrackballReset(Trackball *track);
 void TrackballSetScale(Trackball *track, float scale);
+void TrackballSetRotate(Trackball *track, const float *a);
 void TrackballSetTranslate(Trackball *track, const float *a);
 
+int  TrackballGetModifyCount(Trackball *track);
+
 void TrackballStartDragging(Trackball *track, const float *mousePos, TrackballMode mode);
 void TrackballSetTemporaryRotation(Trackball *track, const float *q);
 void TrackballDrag(Trackball *track, const float *mousePos);
index ff99962..069c4f7 100755 (executable)
@@ -438,6 +438,7 @@ end_of_header
        }
        fp.print " $END\n"
        fp.close
+       return true
   end
 
   def savecom(filename)
@@ -459,6 +460,7 @@ end_of_header
        }
        fp.print "\n"
        fp.close
+       return true
   end
 
   alias :loadgjf :loadcom
index 7f89c41..b39ee86 100644 (file)
@@ -23,7 +23,7 @@ EVT_MENU(wxID_SAVEAS, MyDocManager::OnFileSaveAs)
 END_EVENT_TABLE()
 
 static const char *sReadOnlyTypes[] = {
-       "out", "fchk", "log", NULL
+       "out", "fchk", "log", "dat", "ins", "res", "xyz", NULL
 };
 
 void
index bf53697..e63613b 100755 (executable)
@@ -159,8 +159,8 @@ MyDocument::SetMolecule(Molecule *aMolecule)
        MoleculeView *view = (MoleculeView *)GetFirstView();
        if (view != NULL) {
                MainView_setMolecule(view->mview, aMolecule);
-               if (aMolecule->natoms >= 1000)
-                       view->mview->lineMode = 1;
+       /*      if (aMolecule->natoms >= 1000)
+                       view->mview->lineMode = 1; */
        }
 }
 
@@ -213,9 +213,12 @@ MyDocument::DoOpenDocument(const wxString& file)
        Molecule *newmol;
        p = strdup((const char *)file.mb_str(wxConvFile));
        newmol = MoleculeNew();
+       SetMolecule(newmol);
+       MoleculeRelease(newmol);
        SetUndoEnabled(false);
        if (MolActionCreateAndPerform(newmol, SCRIPT_ACTION("s"), "molload", p) != 0) {
                free(p);
+               SetMolecule(NULL);
                SetUndoEnabled(true);
                return false;
        }
@@ -232,10 +235,14 @@ MyDocument::DoOpenDocument(const wxString& file)
                MoleculeReadExtendedInfo(newmol, p, buf, sizeof buf);
        }
        free(p);
-       SetMolecule(newmol);
        Modify(false);
        GetCommandProcessor()->MarkAsSaved();
        hasFile = true;
+       if (newmol->natoms > 1000)
+               newmol->mview->lineMode = 1;
+       if (TrackballGetModifyCount(newmol->mview->track) == 0)
+               MainView_resizeToFit(newmol->mview);
+       MoleculeCallback_notifyModification(newmol, 0);
        SetUndoEnabled(true);
        return true;
 }
index 4a91ba4..68a2e2e 100755 (executable)
@@ -7,6 +7,7 @@
        objects = {
 
 /* Begin PBXBuildFile section */
+               9E12580F119EC42E00E95DC3 /* docview.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9E12580E119EC42E00E95DC3 /* docview.cpp */; };
                E4299DA70F2A191600780B44 /* ruby_types.c in Sources */ = {isa = PBXBuildFile; fileRef = E4299DA60F2A191600780B44 /* ruby_types.c */; };
                E42D8AF81030193F00C20247 /* MDGraphite.c in Sources */ = {isa = PBXBuildFile; fileRef = E42D8AEF1030193F00C20247 /* MDGraphite.c */; };
                E42D8AF91030193F00C20247 /* MDCore.c in Sources */ = {isa = PBXBuildFile; fileRef = E42D8AF11030193F00C20247 /* MDCore.c */; };
@@ -84,6 +85,7 @@
                29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
                29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
                8D1107320486CEB800E47090 /* Molby.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Molby.app; sourceTree = BUILT_PRODUCTS_DIR; };
+               9E12580E119EC42E00E95DC3 /* docview.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = docview.cpp; path = ../wxSources/docview.cpp; sourceTree = SOURCE_ROOT; };
                E4299DA60F2A191600780B44 /* ruby_types.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ruby_types.c; sourceTree = "<group>"; };
                E42D8AEF1030193F00C20247 /* MDGraphite.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = MDGraphite.c; sourceTree = "<group>"; };
                E42D8AF01030193F00C20247 /* MDGraphite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MDGraphite.h; sourceTree = "<group>"; };
                        isa = PBXGroup;
                        children = (
                                E4A4667410C15B4300E40A9D /* listctrl.cpp */,
+                               9E12580E119EC42E00E95DC3 /* docview.cpp */,
                                E4513F530EED72C0009DF1F7 /* MyListCtrl.h */,
                                E4513F540EED72C0009DF1F7 /* MyListCtrl.cpp */,
                                E4C8CBDD10B83060006C4692 /* MyDocManager.h */,
                                E4C8CBDF10B83060006C4692 /* MyDocManager.cpp in Sources */,
                                E4A4667610C15B4300E40A9D /* listctrl.cpp in Sources */,
                                E4A1802811897F8600B394E7 /* wxKillAddition.cpp in Sources */,
+                               9E12580F119EC42E00E95DC3 /* docview.cpp in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };