1%%
2%%  wings_shape.erl --
3%%
4%%     Utilities for shape records.
5%%
6
7-module(wings_shape).
8
9-export([insert/3]).
10
11-include("wings.hrl").
12
13%%%
14%%% Exported functions.
15%%%
16
17%% new(We, Suffix, St0) -> St.
18%%  Suffix = cut | clone | copy | extract | sep
19%%
20%%  Create a new object based on an old object. The name
21%%  will be created from the old name (with digits and known
22%%  suffixes stripped) with the given Suffix and a number
23%%  appended.
24insert(#we{name=OldName}=We0, Suffix, #st{shapes=Shapes0,onext=Oid}=St) ->
25    Name = new_name(OldName, Suffix, Oid),
26    We = We0#we{id=Oid,name=Name},
27    Shapes = gb_trees:insert(Oid, We, Shapes0),
28    St#st{shapes=Shapes,onext=Oid+1}.
29
30%%%
31%%% Local functions follow.
32%%%
33
34new_name(OldName, Suffix0, Id) ->
35    Suffix = suffix(Suffix0),
36    Base = base(lists:reverse(OldName)),
37    lists:reverse(Base, "_" ++ Suffix ++ integer_to_list(Id)).
38
39%% Note: Filename suffixes are intentionally not translated.
40%% If we are to translate them in the future, base/1 below
41%% must be updated to strip suffixes (both for the current language
42%% and for English).
43
44suffix(cut) -> "cut";
45suffix(clone) -> "clone";
46suffix(copy) -> "copy";
47suffix(extract) -> "extract";
48suffix(mirror) -> "mirror";
49suffix(sep) -> "sep".
50
51%% base_1(ReversedName) -> ReversedBaseName
52%%  Given an object name, strip digits and known suffixes to
53%%  create a base name. Returns the unchanged name if
54%%  no known suffix could be stripped.
55
56base(OldName) ->
57    case base_1(OldName) of
58	error -> OldName;
59	Base -> Base
60    end.
61
62base_1([H|T]) when $0 =< H, H =< $9 -> base_1(T);
63base_1("tuc_"++Base) -> Base;			%"_cut"
64base_1("enolc_"++Base) -> Base;			%"_clone"
65base_1("ypoc_"++Base) -> Base;			%"_copy"
66base_1("tcartxe_"++Base) -> Base;		%"_extract"
67base_1("rorrim_"++Base) -> Base;		%"_mirror"
68base_1("pes_"++Base) -> Base;			%"_sep"
69base_1(_Base) -> error.
70