]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
MEDCouplingUMesh::__getitem__ in python
authorageay <ageay>
Thu, 26 Apr 2012 08:25:01 +0000 (08:25 +0000)
committerageay <ageay>
Thu, 26 Apr 2012 08:25:01 +0000 (08:25 +0000)
src/MEDCoupling_Swig/MEDCoupling.i
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py

index eb7e0fffab87f1d9fc2767d6294d72c9703b8835..9d102204a211962f347dffe758c2d5799f4be53d 100644 (file)
@@ -247,6 +247,7 @@ using namespace INTERP_KERNEL;
 %newobject ParaMEDMEM::MEDCouplingUMesh::getNodalConnectivityIndex;
 %newobject ParaMEDMEM::MEDCouplingUMesh::clone;
 %newobject ParaMEDMEM::MEDCouplingUMesh::__iter__;
+%newobject ParaMEDMEM::MEDCouplingUMesh::__getitem__;
 %newobject ParaMEDMEM::MEDCouplingUMesh::cellsByType;
 %newobject ParaMEDMEM::MEDCouplingUMesh::zipConnectivityTraducer;
 %newobject ParaMEDMEM::MEDCouplingUMesh::buildDescendingConnectivity;
@@ -1109,6 +1110,63 @@ namespace ParaMEDMEM
         return self->cellIterator();
       }
 
+      MEDCouplingPointSet *__getitem__(PyObject *listOrDataArrI) throw(INTERP_KERNEL::Exception)
+      {
+        int sw;
+        int singleVal;
+        std::vector<int> multiVal;
+        std::pair<int, std::pair<int,int> > slic;
+        ParaMEDMEM::DataArrayInt *daIntTyypp=0;
+        int nbc=self->getNumberOfCells();
+        convertObjToPossibleCpp2(listOrDataArrI,nbc,sw,singleVal,multiVal,slic,daIntTyypp);
+        switch(sw)
+          {
+          case 1:
+            {
+              if(singleVal>=nbc)
+                {
+                  std::ostringstream oss;
+                  oss << "Requesting for cell id " << singleVal << " having only " << nbc << " cells !";
+                  throw INTERP_KERNEL::Exception(oss.str().c_str());
+                }
+              if(singleVal>=0)
+                return self->buildPartOfMySelf(&singleVal,&singleVal+1,true);
+              else
+                {
+                  if(nbc+singleVal>0)
+                    {
+                      int tmp=nbc+singleVal;
+                      return self->buildPartOfMySelf(&tmp,&tmp+1,true);
+                    }
+                  else
+                    {
+                      std::ostringstream oss;
+                      oss << "Requesting for cell id " << singleVal << " having only " << nbc << " cells !";
+                      throw INTERP_KERNEL::Exception(oss.str().c_str());
+                    }
+                }
+            }
+          case 2:
+            {
+              return static_cast<MEDCouplingUMesh *>(self->buildPartOfMySelf(&multiVal[0],&multiVal[0]+multiVal.size(),true));
+            }
+          case 3:
+            {
+              MEDCouplingAutoRefCountObjectPtr<DataArrayInt> d0=DataArrayInt::Range(slic.first,slic.second.first,slic.second.second);
+              return self->buildPartOfMySelf(d0->begin(),d0->end(),true);
+            }
+          case 4:
+            {
+              if(!daIntTyypp)
+                throw INTERP_KERNEL::Exception("MEDCouplingUMesh::__getitem__ : null instance has been given in input !");
+              daIntTyypp->checkAllocated();
+              return self->buildPartOfMySelf(daIntTyypp->begin(),daIntTyypp->end(),true);
+            }
+          default:
+            throw INTERP_KERNEL::Exception("MEDCouplingUMesh::__getitem__ : unrecognized type in input ! Possibilities are : int, list or tuple of int DataArrayInt instance !");
+          }
+      }
+
       void insertNextCell(INTERP_KERNEL::NormalizedCellType type, int size, PyObject *li) throw(INTERP_KERNEL::Exception)
       {
         int sz;
index 56436244ee70c43d108746ef27c7d826877127e0..45021fc730127306ffdb5148b5f2cdd28da34fe8 100644 (file)
@@ -1421,7 +1421,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         expected1=[0,1]
         self.assertEqual(2,arr1.getNumberOfTuples());
         self.assertEqual(1,arr1.getNumberOfComponents());
-        self.assertTrue(expected1,arr1.getValues());
+        self.assertEqual(expected1,arr1.getValues());
         expected2=[0,3,4,1,2]
         arr1=m2_1.rearrange2ConsecutiveCellTypes();
         self.assertEqual(5,arr1.getNumberOfTuples());
@@ -2596,7 +2596,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertEqual(5,len(types));
         expected1=[NORM_POLYGON, NORM_TRI3, NORM_QUAD4, NORM_TRI6, NORM_QUAD8]
         expected1.sort()
-        self.assertTrue(expected1==types);
+        self.assertEqual(expected1,types);
         self.assertTrue(mesh.isPresenceOfQuadratic());
         self.assertEqual(62,mesh.getMeshLength());
         f1=mesh.getMeasureField(False);
@@ -2613,7 +2613,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertEqual(3,len(types2));
         expected2=[NORM_POLYGON, NORM_TRI3, NORM_QUAD4]
         expected2.sort()
-        self.assertTrue(expected2==types2);
+        self.assertEqual(expected2,types2);
         pass
 
     def testCheckGeoEquivalWith(self):
@@ -8518,7 +8518,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertEqual(10,ret.getNbOfElems());
         self.assertEqual(7,newNbTuple);
         self.assertEqual(1,ret.getNumberOfComponents());
-        self.assertTrue(expected,ret.getValues());
+        self.assertEqual(expected,ret.getValues());
         pass
 
     def testDADIReverse1(self):
@@ -8798,8 +8798,8 @@ class MEDCouplingBasicsTest(unittest.TestCase):
             pass
         self.assertEqual(60,m12.getNodalConnectivity().getNumberOfTuples());
         self.assertEqual(9,m12.getNodalConnectivityIndex().getNumberOfTuples());
-        self.assertTrue(expected3,m12.getNodalConnectivity().getValues());
-        self.assertTrue(expected4,m12.getNodalConnectivityIndex().getValues());
+        self.assertEqual(expected3,m12.getNodalConnectivity().getValues());
+        self.assertEqual(expected4,m12.getNodalConnectivityIndex().getValues());
         pass
 
     def testIntersect2DMeshesTmp4(self):
@@ -9774,30 +9774,30 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         expected1=[2,9,16]
         self.assertEqual(3,d.getNumberOfTuples());
         self.assertEqual(1,d.getNumberOfComponents());
-        self.assertTrue(expected1,d.getValues());
+        self.assertEqual(expected1,d.getValues());
         #
         d=DataArrayInt.Range(2,23,7);
         self.assertEqual(3,d.getNumberOfTuples());
         self.assertEqual(1,d.getNumberOfComponents());
-        self.assertTrue(expected1,d.getValues());
+        self.assertEqual(expected1,d.getValues());
         #
         d=DataArrayInt.Range(2,24,7);
         expected2=[2,9,16,23]
         self.assertEqual(4,d.getNumberOfTuples());
         self.assertEqual(1,d.getNumberOfComponents());
-        self.assertTrue(expected2,d.getValues());
+        self.assertEqual(expected2,d.getValues());
         #
         d=DataArrayInt.Range(24,2,-7);
         expected3=[24,17,10,3]
         self.assertEqual(4,d.getNumberOfTuples());
         self.assertEqual(1,d.getNumberOfComponents());
-        self.assertTrue(expected3,d.getValues());
+        self.assertEqual(expected3,d.getValues());
         #
         d=DataArrayInt.Range(23,2,-7);
         expected4=[23,16,9]
         self.assertEqual(3,d.getNumberOfTuples());
         self.assertEqual(1,d.getNumberOfComponents());
-        self.assertTrue(expected4,d.getValues());
+        self.assertEqual(expected4,d.getValues());
         #
         d=DataArrayInt.Range(23,22,-7);
         self.assertEqual(1,d.getNumberOfTuples());
@@ -9823,6 +9823,32 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertRaises(InterpKernelException,DataArrayInt.Range,22,23,0);
         pass
 
+    def testSwigUMeshGetItem1(self):
+        m=MEDCouplingDataForTest.build2DTargetMesh_1();
+        subMesh=m.buildPartOfMySelf([1,3],True);
+        self.assertTrue(isinstance(subMesh,MEDCouplingUMesh))
+        m1=m[[1,3]]
+        self.assertTrue(isinstance(m1,MEDCouplingUMesh))
+        m2=m[(1,3)]
+        self.assertTrue(isinstance(m2,MEDCouplingUMesh))
+        m3=m[1::2]
+        self.assertTrue(isinstance(m3,MEDCouplingUMesh))
+        m4=m[DataArrayInt.New([1,3])]
+        m5_1=m[1]
+        self.assertTrue(isinstance(m5_1,MEDCouplingUMesh))
+        m5_2=m[3]
+        self.assertTrue(isinstance(m5_2,MEDCouplingUMesh))
+        m5=MEDCouplingUMesh.MergeUMeshesOnSameCoords([m5_1,m5_2]);
+        m5.setName(subMesh.getName())
+        self.assertTrue(isinstance(m4,MEDCouplingUMesh))
+        self.assertTrue(subMesh.isEqual(m1,1e-12))
+        self.assertTrue(subMesh.isEqual(m2,1e-12))
+        self.assertTrue(subMesh.isEqual(m3,1e-12))
+        self.assertTrue(subMesh.isEqual(m4,1e-12))
+        self.assertTrue(subMesh.isEqual(m5,1e-12))
+        self.assertRaises(InterpKernelException,m.buildPartOfMySelf,[1,5],True);
+        pass
+
     def setUp(self):
         pass
     pass