~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Mod/Points/App/AppPointsPy.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) Juergen Riegel         <juergen.riegel@web.de>          *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
6
 *   This library is free software; you can redistribute it and/or         *
 
7
 *   modify it under the terms of the GNU Library General Public           *
 
8
 *   License as published by the Free Software Foundation; either          *
 
9
 *   version 2 of the License, or (at your option) any later version.      *
 
10
 *                                                                         *
 
11
 *   This library  is distributed in the hope that it will be useful,      *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU Library General Public License for more details.                  *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU Library General Public     *
 
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
 
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
 
 
24
#include "PreCompiled.h"
 
25
#ifndef _PreComp_
 
26
#       include <stdio.h>
 
27
# if defined (_POSIX_C_SOURCE)
 
28
#   undef  _POSIX_C_SOURCE
 
29
# endif // (re-)defined in pyconfig.h
 
30
#       include <Python.h>
 
31
#endif
 
32
 
 
33
#include <Base/Console.h>
 
34
#include <Base/Interpreter.h>
 
35
#include <Base/FileInfo.h>
 
36
 
 
37
#include <App/Application.h>
 
38
#include <App/Document.h>
 
39
#include <App/Feature.h>
 
40
#include <App/Property.h>
 
41
 
 
42
#include "Points.h"
 
43
#include "PointsPy.h"
 
44
#include "PointsAlgos.h"
 
45
#include "FeaturePointsImportAscii.h"
 
46
 
 
47
using namespace Points;
 
48
 
 
49
/* module functions */
 
50
static PyObject *                        
 
51
open(PyObject *self, PyObject *args)     
 
52
{                                        
 
53
  const char* Name;
 
54
  if (! PyArg_ParseTuple(args, "s",&Name))                       
 
55
    return NULL;                         
 
56
 
 
57
  PY_TRY {
 
58
    
 
59
    Base::Console().Log("Open in Points with %s",Name);
 
60
    Base::FileInfo file(Name);
 
61
 
 
62
    // extract ending
 
63
    if(file.extension() == "")
 
64
      Py_Error(PyExc_Exception,"no file ending");
 
65
 
 
66
    if(file.hasExtension("asc"))
 
67
    {
 
68
      // create new document and add Import feature
 
69
      App::Document *pcDoc = App::GetApplication().newDocument("Unnamed");
 
70
      Points::Feature *pcFeature = (Points::Feature *)pcDoc->addObject("Points::Feature", file.fileNamePure().c_str());
 
71
      Points::PointKernel pkTemp;
 
72
      pkTemp.load(Name);
 
73
      pcFeature->Points.setValue( pkTemp );
 
74
 
 
75
    }
 
76
    else
 
77
    {
 
78
      Py_Error(PyExc_Exception,"unknown file ending");
 
79
    }
 
80
  } PY_CATCH;
 
81
 
 
82
        Py_Return;    
 
83
}
 
84
 
 
85
static PyObject *                        
 
86
insert(PyObject *self, PyObject *args)     
 
87
{                                        
 
88
  const char* Name;
 
89
  const char* DocName;
 
90
  if (! PyArg_ParseTuple(args, "ss",&Name,&DocName))                     
 
91
    return NULL;                         
 
92
 
 
93
  PY_TRY {
 
94
    
 
95
    Base::Console().Log("Import in Points with %s",Name);
 
96
    Base::FileInfo file(Name);
 
97
 
 
98
    // extract ending
 
99
    if(file.extension() == "")
 
100
      Py_Error(PyExc_Exception,"no file ending");
 
101
 
 
102
    if(file.hasExtension("asc"))
 
103
    {
 
104
      // add Import feature
 
105
      App::Document *pcDoc = App::GetApplication().getDocument(DocName);
 
106
      if (!pcDoc)
 
107
      {
 
108
        PyErr_Format(PyExc_Exception, "Import called to the non-existing document '%s'", DocName);
 
109
        return NULL;
 
110
      }
 
111
 
 
112
      Points::Feature *pcFeature = (Points::Feature *)pcDoc->addObject("Points::Feature", file.fileNamePure().c_str());
 
113
      Points::PointKernel pkTemp;
 
114
      pkTemp.load(Name);
 
115
      pcFeature->Points.setValue( pkTemp );
 
116
    }
 
117
    else
 
118
    {
 
119
      Py_Error(PyExc_Exception,"unknown file ending");
 
120
    }
 
121
  } PY_CATCH;
 
122
 
 
123
        Py_Return;    
 
124
}
 
125
 
 
126
static PyObject * 
 
127
show(PyObject *self, PyObject *args)
 
128
{
 
129
    PyObject *pcObj;
 
130
    if (!PyArg_ParseTuple(args, "O!", &(PointsPy::Type), &pcObj))     // convert args: Python->C
 
131
        return NULL;                             // NULL triggers exception
 
132
 
 
133
    PY_TRY {
 
134
        App::Document *pcDoc = App::GetApplication().getActiveDocument();        
 
135
        if (!pcDoc)
 
136
            pcDoc = App::GetApplication().newDocument();
 
137
        PointsPy* pPoints = static_cast<PointsPy*>(pcObj);
 
138
        Points::Feature *pcFeature = (Points::Feature *)pcDoc->addObject("Points::Feature", "Points");
 
139
        // copy the data
 
140
        //TopoShape* shape = new MeshObject(*pShape->getTopoShapeObjectPtr());
 
141
        pcFeature->Points.setValue(*(pPoints->getPointKernelPtr()));
 
142
        //pcDoc->recompute();
 
143
    } PY_CATCH;
 
144
 
 
145
    Py_Return;
 
146
}
 
147
 
 
148
// registration table  
 
149
struct PyMethodDef Points_Import_methods[] = {
 
150
    {"open",  open,   1},                               /* method name, C func ptr, always-tuple */
 
151
    {"insert",insert, 1},
 
152
    {"show",show, 1},
 
153
 
 
154
    {NULL, NULL}                /* end of table marker */
 
155
};