You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 Jon Evans <jon@craftyjon.com>
  5. * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <magic_enum.hpp>
  21. #include <api/api_utils.h>
  22. namespace kiapi::common
  23. {
  24. std::optional<KICAD_T> TypeNameFromAny( const google::protobuf::Any& aMessage )
  25. {
  26. static const std::map<std::string, KICAD_T> s_types = {
  27. { "type.googleapis.com/kiapi.board.types.Track", PCB_TRACE_T },
  28. { "type.googleapis.com/kiapi.board.types.Arc", PCB_ARC_T },
  29. { "type.googleapis.com/kiapi.board.types.Via", PCB_VIA_T },
  30. { "type.googleapis.com/kiapi.board.types.Text", PCB_TEXT_T },
  31. { "type.googleapis.com/kiapi.board.types.TextBox", PCB_TEXTBOX_T },
  32. { "type.googleapis.com/kiapi.board.types.GraphicShape", PCB_SHAPE_T },
  33. { "type.googleapis.com/kiapi.board.types.Pad", PCB_PAD_T },
  34. { "type.googleapis.com/kiapi.board.types.Zone", PCB_ZONE_T },
  35. { "type.googleapis.com/kiapi.board.types.Dimension", PCB_DIMENSION_T },
  36. { "type.googleapis.com/kiapi.board.types.ReferenceImage", PCB_REFERENCE_IMAGE_T },
  37. { "type.googleapis.com/kiapi.board.types.Group", PCB_GROUP_T },
  38. { "type.googleapis.com/kiapi.board.types.Field", PCB_FIELD_T },
  39. { "type.googleapis.com/kiapi.board.types.FootprintInstance", PCB_FOOTPRINT_T },
  40. };
  41. auto it = s_types.find( aMessage.type_url() );
  42. if( it != s_types.end() )
  43. return it->second;
  44. return std::nullopt;
  45. }
  46. LIB_ID LibIdFromProto( const types::LibraryIdentifier& aId )
  47. {
  48. return LIB_ID( aId.library_nickname(), aId.entry_name() );
  49. }
  50. types::LibraryIdentifier LibIdToProto( const LIB_ID& aId )
  51. {
  52. types::LibraryIdentifier msg;
  53. msg.set_library_nickname( aId.GetLibNickname() );
  54. msg.set_entry_name( aId.GetLibItemName() );
  55. return msg;
  56. }
  57. void PackVector2( kiapi::common::types::Vector2& aOutput, const VECTOR2I aInput )
  58. {
  59. aOutput.set_x_nm( aInput.x );
  60. aOutput.set_y_nm( aInput.y );
  61. }
  62. VECTOR2I UnpackVector2( const types::Vector2& aInput )
  63. {
  64. return VECTOR2I( aInput.x_nm(), aInput.y_nm() );
  65. }
  66. } // namespace kiapi::common