内容表

关于

Filename is a special class to handle filenames and paths. It is used to easily construct and edit paths, filenames and suffixes. It also allows to open dialogs to select files and directories.

警告
To handle files with the MAXON API see also Url Manual .
注意
To convert a Filname to a maxon::Url use MaxonConvert() .

Access

Filename objects can be created on demand or can be obtained from other entities.

To retrieve and modify Filename objects stored in a BaseContainer respectively use:

另请参阅 BaseContainer Manual .

To retrieve and modify Filename objects stored in a GeData object ( GeData type is DA_FILENAME ) respectively use:

另请参阅 GeData Manual .

Important paths are obtained from:

// This example reads the filename parameter of an alembic generator.

// check if the active object is an "Alembic Generator" BaseObject * const object = doc-> GetActiveObject (); if ( object == nullptr && object-> GetType () != Oalembicgenerator ) return maxon::IllegalArgumentError( MAXON_SOURCE_LOCATION );

GeData data;

// read the "Path to Alembic file" parameter if (!object-> GetParameter ( DescID ( ALEMBIC_PATH ), data, DESCFLAGS_GET::NONE )) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION ); ApplicationOutput ( "Alembic File: " + data. GetFilename (). GetString ());

Combine

A Filename can be constructed with basic operators:

注意
Only the last part (typically the file) of the given Filename is added.
// This example lets the user select a folder. // A full file path is constructed and the example checks if that file exists. Filename selectFolder;

// open a folder selector dialog if (!selectFolder. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::DIRECTORY , "Select Folder" _s)) return maxon::OK ; const Filename fullFilePath = selectFolder + "cube.c4d" ;

// check if given file exists if ( GeFExist (fullFilePath)) ApplicationOutput ( "The folder contains \"cube.c4d\"" ); else ApplicationOutput ( "The folder does not container \"cube.c4d\"" );

// This example constructs the Filename for an image file // in the plugin's "res" folder and loads this image.

// construct full file name const Filename imageFile = GeGetPluginPath () + Filename ( "res" ) + Filename ( "arbitrary_icon.tif" );

// check if the file exists if (! GeFExist (imageFile)) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION ); AutoAlloc<BaseBitmap> bitmap; if (bitmap == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );

// load image file into the given BaseBitmap if (bitmap-> Init (imageFile) != IMAGERESULT::OK ) return maxon::UnknownError( MAXON_SOURCE_LOCATION );

// show BaseBitmap in the Picture Viewer ShowBitmap (bitmap);

拷贝

A Filename object can easily be copied:

特性

String

The content of a Filename object can be represented by a String :

另请参阅 String Manual (Classic) .

// This example prints the full path to the console. const String filenameString = selectedFile.GetString(); ApplicationOutput ( "File selected: " + filenameString);

File and Directory

A filename may be composed of a directory part and a file part. Each part can be edited independently:

// This example prints the file and the directory to the console. const String file = selectedFile.GetFileString(); const String directory = selectedFile.GetDirectory().GetString(); ApplicationOutput (directory + " - " + file);

Suffix

Part of the filename may also be the file suffix. This suffix can be edited separately:

// This example checks the suffix of the given filename.

// check if the given Filename references a Cinema 4D scene file if (selectedFile.CheckSuffix( "c4d" _s)) ApplicationOutput ( "C4D File" ); else ApplicationOutput ( "Some other file" );

It is possible to get the suffix for a given image file type:

// This example adds the correct suffix to the given Filename object // based on the image file type. imageFileName = GeFilterSetSuffix (imageFileName, FILTER_JPG ); bitmap-> Save (imageFileName, FILTER_JPG , nullptr , SAVEBIT::NONE );

Memory Mode

The memory mode allows to perform read and write operations on a memory block instead of a file on the hard drive. This is typically used to encrypt or compress the resulting data.

// This example writes some data into memory and accesses the raw memory afterwards. AutoAlloc<MemoryFileStruct> mfs; if (mfs == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION ); Filename fn; fn. SetMemoryWriteMode (mfs); void * data = nullptr ; Int size = 0;

// open HyperFile to write if (!hyperFile->Open(0, fn, FILEOPEN::WRITE , FILEDIALOG::NONE )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); hyperFile->WriteInt32(123); hyperFile->WriteString( "foo" _s); hyperFile->WriteString( "bar" _s); hyperFile->WriteInt32(456); hyperFile->Close();

// get raw data mfs-> GetData (data, size, false );

注意
The data stored in memory may be written to a file using HyperFile::WriteMemory() .

Functionality

Select Files and Folders

A Filename object is also useful to open dialogs to select files and folders:

The files that can be selected in a dialog can be filtered with these flags:

The type of dialog is defined with this flag:

// This example shows how to select a file for loading, saving and a folder. Filename loadFile;

// open a file selector dialog to open a file if (loadFile. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::LOAD , "Load File" _s)) ApplicationOutput ( "File to load: " + loadFile. GetString ()); Filename saveFile;

// open a file selector dialog to save a file if (saveFile. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::SAVE , "Save File" _s)) ApplicationOutput ( "File to save: " + saveFile. GetString ()); Filename folder;

// open a folder selector dialog if (folder. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::DIRECTORY , "Select Folder" _s)) ApplicationOutput ( "Folder: " + folder. GetString ());

Preset Browser

A Filename object may also be used to reference a file located in Cinema 4D 's content browser.

// This example checks if the file referenced in a filename parameter is a preset file or not. GeData geData;

// access a filename parameter if (node-> GetParameter ( DescID (ID_FILENAME), geData, DESCFLAGS_GET::NONE )) { const Filename filename = geData. GetFilename ();

// check if the given Filename // is a preset URL if (filename. IsBrowserUrl ()) ApplicationOutput ( "File is stored in presets" _s); }

比较

Two Filename objects can be compared easily:

// This example checks if the selected folder is the desktop. const Filename desktop = GeGetC4DPath ( C4D_PATH_DESKTOP ); Filename folder;

// open folder selector dialog if (folder. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::DIRECTORY , "Select Folder" _s)) { if (folder == desktop) ApplicationOutput ( "You selected the desktop." ); else ApplicationOutput ( "You selected " + folder. GetString ()); }

Disc I/O

A Filename object can be stored in a BaseFile HyperFile using:

// This example writes the Filename into a new BaseFile. AutoAlloc<BaseFile> bf; if (bf == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );

// open BaseFile to write if (!bf-> Open (filename, FILEOPEN::WRITE , FILEDIALOG::ANY )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); bf-> WriteFilename (filenameData); bf-> 关闭 ();

// This example reads a String form the given BaseFile. AutoAlloc<BaseFile> bf; if (bf == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );

// open BaseFile to read if (!bf-> Open (filename, FILEOPEN::READ , FILEDIALOG::ANY )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); Filename filenameData; bf-> ReadFilename (&filenameData); ApplicationOutput ( "Filename from BaseFile: " + filenameData. GetString ()); bf-> 关闭 ();

// This example writes the Filename into a new HyperFile. AutoAlloc<HyperFile> hf; if (hf == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );

// open HyperFile to write if (!hf-> Open (753, filename, FILEOPEN::WRITE , FILEDIALOG::ANY )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); hf-> WriteFilename (filenameData); hf-> 关闭 ();

// This example reads a String from the given HyperFile. AutoAlloc<HyperFile> hf; if (hf == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );

// open HyperFile to read if (!hf-> Open (753, filename, FILEOPEN::READ , FILEDIALOG::ANY )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); Filename filenameData; hf-> ReadFilename (&filenameData); ApplicationOutput ( "Filename from HyperFile: " + filenameData. GetString ());

延伸阅读

BaseFile::Close
Bool Close()
FILESELECT::DIRECTORY
@ DIRECTORY
Folder selection dialog.
IMAGERESULT::OK
@ OK
Image loaded/created.
FILEDIALOG::NONE
@ NONE
Never show an error dialog.
Int
maxon::Int Int
定义: ge_sys_math.h:62
FILESELECT::SAVE
@ SAVE
Save dialog.
GeFExist
Bool GeFExist(const Filename &name, Bool isdir=false)
Filename::IsBrowserUrl
Bool IsBrowserUrl() const
BaseObject
定义: c4d_baseobject.h:224
DescID
定义: lib_description.h:327
FILEDIALOG::ANY
@ ANY
Show an error dialog for any error.
GeGetPluginPath
const Filename GeGetPluginPath(void)
Oalembicgenerator
#define Oalembicgenerator
Alembic generator.
定义: ge_prepass.h:1086
MemoryFileStruct::GetData
void GetData(void *&data, Int &size) const
BaseFile::ReadFilename
Bool ReadFilename(Filename *v)
Filename
Manages file and path names.
定义: c4d_file.h:93
maxon::OK
return OK
定义: apibase.h:2532
FILESELECTTYPE::ANYTHING
@ ANYTHING
Any file.
C4D_PATH_DESKTOP
#define C4D_PATH_DESKTOP
OS desktop directory.
定义: c4d_file.h:1908
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
HyperFile::WriteFilename
Bool WriteFilename(const Filename &v)
BaseFile::Open
Bool Open(const Filename &name, FILEOPEN mode=FILEOPEN::READ, FILEDIALOG error_dialog=FILEDIALOG::IGNOREOPEN, BYTEORDER order=BYTEORDER::V_MOTOROLA, Int32 type='C4DC', Int32 creator='C4D1')
BaseBitmap::Init
static IMAGERESULT Init(BaseBitmap *&res, const Filename &name, Int32 frame=-1, Bool *ismovie=nullptr, BitmapLoaderPlugin **loaderplugin=nullptr, const maxon::Delegate< void(Float progress)> &progressCallback=nullptr)
HyperFile::Open
Bool Open(Int32 ident, const Filename &filename, FILEOPEN mode, FILEDIALOG error_dialog)
Filename::GetString
String GetString(void) const
String
定义: c4d_string.h:38
FILEOPEN::WRITE
@ WRITE
GeGetC4DPath
const Filename GeGetC4DPath(Int32 whichpath)
GeData
定义: c4d_gedata.h:82
GeData::GetFilename
const Filename & GetFilename(void) const
定义: c4d_gedata.h:475
ApplicationOutput
#define ApplicationOutput(formatString,...)
定义: debugdiagnostics.h:207
ShowBitmap
Bool ShowBitmap(const Filename &fn)
FILESELECT::LOAD
@ LOAD
Load dialog.
Filename::SetMemoryWriteMode
void SetMemoryWriteMode(MemoryFileStruct *mfs)
FILEOPEN::READ
@ READ
Open the file for reading.
FILTER_JPG
#define FILTER_JPG
JPEG.
定义: ge_prepass.h:183
HyperFile::Close
Bool Close()
AutoAlloc
定义: ge_autoptr.h:36
DESCFLAGS_GET::NONE
@ NONE
None.
GeFilterSetSuffix
Filename GeFilterSetSuffix(const Filename &name, Int32 id)
BaseDocument::GetActiveObject
BaseObject * GetActiveObject(void)
HyperFile::ReadFilename
Bool ReadFilename(Filename *v)
BaseBitmap::Save
IMAGERESULT Save(const Filename &name, Int32 format, BaseContainer *data, SAVEBIT savebits) const
C4DAtom::GetType
Int32 GetType() const
定义: c4d_baselist.h:1348
C4DAtom::GetParameter
Bool GetParameter(const DescID &id, GeData &t_data, DESCFLAGS_GET flags)
ALEMBIC_PATH
@ ALEMBIC_PATH
定义: Oalembicgenerator.h:6
BaseFile::WriteFilename
Bool WriteFilename(const Filename &v)
SAVEBIT::NONE
@ NONE
None.
Filename::FileSelect
Bool FileSelect(FILESELECTTYPE type, FILESELECT flags, const maxon::String &title, const maxon::String &force_suffix=maxon::String())

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1